Eglot based Emacs C++ IDE with clangd
2019-01-07
I have an old post documenting my first
attempt at turning Emacs into a C++ IDE with clangd. That post
describes using two packages: lsp-mode and lsp-clangd. Those
packages have evolved and now clangd usage is built into lsp-mode,
so the post is a bit outdated. I've also started to use
Eglot (see previous post for my
Eglot Python IDE). So, let's put together an updated setup:
Requirements
First, one needs to have clangd installed. These days, the 8.0
release of LLVM is a few months away, but clangd (part of the
clang-tools-extra LLVM project) is in rapid development and the
master of the repository should be used. The installation
instructions
from the LLVM documentation are easy to follow.
My C++ development happens on multiple machines. In my Emacs
configuration I keep a simple variable around to point to wherever
clangd is installed on various machines.
By default I'm letting Emacs find it, but I have things like this
sprinkled around my configuration (pointing to a specific LLVM
installation not in my PATH):
Eglot setup
Eglot uses project.el, but I use
Projectile, so I start by
defining a function that will tell project.el to find a project via
Projectile, thanks @wyuenho on
GitHub:
Now I have a function I call when I'm ready to start digging into a
C++ project which has an associated
compile_commands.json:
- Ensure that Eglot is installed via
use-package. - Ensure that the
completion-at-pointbackend is used bycompany(bring it to the front of thecompany-backendslist). - Ensure that
project.eluses Projectile to find my project definition (this is because I usually have C++ projects using git submodules). - Add my
clangdexecutable to theeglot-server-programslist. - Add the hook to automatically start Eglot.
If I don't want the hook anymore, I use this very simple function: