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 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:
C-c C-l (c-toggle-electric-state)
: this toggles electric mode, which is what causes certain keys (like ;
) to cause auto-frmatting to occur. You might not want this if you're just doing a few changes into code that doesn't match the formatting preferences you have.M-;
: insert a comment at the end of the current line, if none is there already.C-c C-c
: comment selected block; C-u C-c C-c
to uncomment. Possibly because of CUA bindings, I use C-c C-c C-c
instead.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).
s-p f
: Display a list of all files in the project, filter down by typing for match.s-p d
: Display a list of all directories in the project, filter down by typing for match.s-p D
: Opens the root of the project in dired.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 is a client/server application that indexes C/C++ code and keeps a persistent file-based database of references, declarations, definitions, symbolnames etc.
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 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 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 is a very straightforward LSP that I had better luck configuring than lsp-mode
.
Here are some handy commands:
M-x eglot-format
asks the server to format buffer or the active region;M-x eglot-code-actions
asks the server for any code actions at point. These may tipically be simple fixes, like deleting an unused variable, or fixing an import. Left click on diagnostics to check if there are any there;M-x eglot-help-at-point
asks the server for help for symbol at point.There are two things you'll need to set up properly to get good clang-based code assistance.
clangd
, which is theI 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!