Emacs and C

There are many packages that can help you program in C and C++ when working with emacs.

I've collected some of them on these page.

My personal combo is cc-mode, projectile, company, eglot, and clangd as the language server.

CC Mode

CC Mode is installed by default with emacs, so any keybidings you pick will be usable on any installation.

This is a very 'structural' approach, with no semantic information available.

See more information here.

Some handy commands to know about:

Projectile

projectile aims to provide a nice set of features operating on a project level without introducing external dependencies. This is nice if you work on Windows, as some packages struggle with missing utilities.

User manual available here.

Some handy keybidings, with s-p being the prefix (I have it as C-c p for example).

Company

Company is a text completion framework for Emacs. The name stands for "complete anything". It uses pluggable back-ends and front-ends to retrieve and display completion candidates.

I recommend using the clandg language server - see Setting up clangd below.

RTags

RTags is a client/server application that indexes C/C++ code and keeps a persistent file-based database of references, declarations, definitions, symbolnames etc.

flycheck

Flycheck is a modern on-the-fly syntax checking extension for GNU Emacs, intended as replacement for the older Flymake extension which is part of GNU Emacs.

lsp-mode

lsp-mode is a client for a language server protocol (a process that can answer questions about your project), and aims to provide IDE-like experience by providing optional integration with the most popular Emacs packages like company, flycheck and projectile.

cmake-ide

cmake-ide uses cmake to configure other packages for you based on the information that cmake already generates.

Unfortunately, I've been unable to install this successfully on my machine, so no first-hand experience here.

eglot

eglot is a very straightforward LSP that I had better luck configuring than lsp-mode.

Here are some handy commands:

Setting up clangd

There are two things you'll need to set up properly to get good clang-based code assistance.

I was going to write a section on each, but the instructions on clangd.llvm.org are clear and concise.

It has been very simple to get this going on a CMake-based project, the instructions for that are included there as well (basically cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 and put the compile_commands.json file on a parent directory of where your source files are).

Happy programming!

Tags:  codingcppemacs

Home