Setting Up clangd for Linux Kernel and Driver Development

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

  1. Why Use clangd?
  2. Checking clangd Installation
  3. Generating compile_commands.json for Kernel Development
  4. Handling Compiler Mismatch Warnings
  5. Configuring VSCode for Remote SSH Development
  6. Stopping clangd if Needed
  7. Troubleshooting


Why Use clangd?

clangd enhances productivity by providing:

  • Code Navigation: "Go to Definition," "Find References," and "Symbol Navigation."
  • Code Completion: Intelligent suggestions for functions, variables, and keywords.
  • Diagnostics: Real-time error checking and warnings.
  • Performance: Fast and memory-efficient analysis, even for large projects.


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:

  • Check the kernel’s compiler:

 uname -r # Get kernel version 
dpkg -l | grep gcc # List installed GCC versions        

  • Use the same compiler as the kernel:

export CROSS_COMPILE=aarch64-linux-gnu- # Example for ARM64
 make ARCH=arm64        

  • Suppress the warning (optional):export ACTION_kernelbuild=1


Configuring VSCode for Remote SSH Development

To enable clangd features in VSCode over SSH:

  1. Install clangd on the remote machine:sudo apt install clangd -y
  2. Install the clangd extension in VSCode:Search for "clangd" in the Extensions Marketplace and install it.
  3. Place compile_commands.json in your project root:Ensure the file is generated and accessible by VSCode.
  4. Configure VSCode settings (optional): Create a .vscode/settings.json file in your project:{ "C_Cpp.intelliSenseEngine": "Clangd", "clangd.path": "/usr/bin/clangd", "clangd.arguments": [ "--compile_flags_from_file=compile_commands.json" ] }
  5. Reload VSCode:Use Ctrl+Shift+P > "Developer: Reload Window" to apply changes.


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

  • Check compile_commands.json:Ensure it exists in the project root and contains valid entries.
  • Verify clangd is running:Open the Output panel (View > Output) > select "clangd" to check for errors.
  • Reload VSCode:Use "Developer: Reload Window" to restart extensions.

2. Compiler Mismatch Warning

  • Use the same compiler as the kernel:export CROSS_COMPILE=/path/to/kernel/compiler
  • Suppress the warning:export ACTION_kernelbuild=1

3. Missing Kernel Headers

  • Install kernel headers:sudo apt install linux-headers-$(uname -r) -y
  • Add include paths to compile_commands.json:-I/usr/src/linux-headers-$(uname -r)/include


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!



要查看或添加评论,请登录

David Zhu的更多文章