Setting Up clangd for Linux Kernel and Driver Development
clangd is a language server that provides advanced features like "Go to Definition", code completion, diagnostics, and error checking for C/C++ development. While clangd is built on the Clang compiler infrastructure, it can work seamlessly with GCC-based projects by leveraging a compile_commands.json file. This guide explains how to set up clangd for Linux kernel and driver development, including generating compile_commands.json, resolving compiler mismatch warnings, and configuring VSCode for remote SSH editing.
Table of Contents
Why Use clangd?
clangd enhances productivity by providing:
Checking clangd Installation
Verify if clangd is installed on your system:
clangd --version
Ubuntu clangd version 14.0.0-1ubuntu1.1
Features: linux+grpc
Platform: x86_64-pc-linux-gnu
If not installed, install it:
sudo apt install clangd -y # Debian/Ubuntu
For Windows users with VSCode, install clangd via the VSCode Extensions Marketplace.
Generating compile_commands.json for Linux Kernel Development
clangd relies on compile_commands.json to understand how each source file is compiled. For kernel development, generate this file using one of the following methods:
Method 1: Using bear (Recommended for Out-of-Tree Kernel Modules)
Install bear:
sudo apt install bear -y
bear --version
bear 3.0.18
Clean the build directory and generate compile_commands.json:
make clean
bear -- make
ls | grep compile_commands.json
compile_commands.json
vi compile_commands.json
[
{
"arguments": [
"/usr/bin/gcc-12",
"-Wp,-MMD,/home/firmwarezhu/linux/ch341/ch341-i2c-spi-gpio/.ch341-core.o.d",
"-nostdinc",
"-I./arch/x86/include",
"-I./arch/x86/include/generated",
"-I./include",
"-I./arch/x86/include/uapi",
"-I./arch/x86/include/generated/uapi",
"-I./include/uapi",
"-I./include/generated/uapi",
"-include",
...
This captures compilation commands during the build and generates compile_commands.json.
Method 2: Using gen_compile_commands.py (In-Tree Kernel Development)
Build the kernel with verbose output and save the log:
make V=1 | tee build.log
Generate compile_commands.json using the script from the kernel source tree:
python3 scripts/clang-tools/gen_compile_commands.py
This extracts compiler flags from build.log and creates compile_commands.json.
Handling Compiler Mismatch Warnings
When building kernel modules, you might see:
warning: the compiler differs from the one used to build the kernel
To resolve this:
uname -r # Get kernel version
dpkg -l | grep gcc # List installed GCC versions
export CROSS_COMPILE=aarch64-linux-gnu- # Example for ARM64
make ARCH=arm64
Configuring VSCode for Remote SSH Development
To enable clangd features in VSCode over SSH:
Stopping clangd if Needed
To stop clangd processes:
pkill clangd # Kills all running clangd instances
Or find the PID manually and kill it:
ps aux | grep clangd
kill -9 <PID>
Troubleshooting
1. "Go to Definition" Not Working
2. Compiler Mismatch Warning
3. Missing Kernel Headers
Conclusion
By setting up clangd with compile_commands.json, you can leverage advanced code navigation and analysis features in VSCode for Linux kernel and driver development. This setup ensures smooth remote development over SSH and resolves common issues like compiler mismatch warnings. If you encounter specific errors, check the clangd output logs and verify the compile_commands.json configuration. Let me know if you need further assistance!