Skip to content

How To Setup clangd

Mark Carlin edited this page Mar 18, 2025 · 2 revisions

Why

Using clangd will compile the kernel in the background and show all errors and warning, similar to ElixirLS. It's much easier to find and fix bugs while still in the editor, as the alternative is to build the kernel with buildroot, then examine the output for files and line numbers for editing from the separate console window.

How

Install in WSL the clangd extension: ScreenHunter_266 Mar  17 14 06 The configuration for clangd is stored in the linux.code-workspace file, so be sure to use the workspace when working on the kernel. Note the linux.code-workspace file is for WSL, if running Linux use the linux-linux.code-workspace file instead. There is also some configuration stored in the .clangd file at the root, for now this is used to exclude some gcc warnings that clang doesn't recognize.

First you must be able to cross-compile the kernel, see the wiki page. Then use bear to generate the compile_commands.json file that is used by the clangd extension.

If bear is not installed, install it with:

sudo apt update
sudo apt install -y bear

Then create the compile-commands.json file:

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- clean
bear -- make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- all

Note the compile-commands.json and .clangd files are stored as part of the git archive, so you'll only need to regenerate on kernel updates or additions.

Clone this wiki locally