A tool to automatically generate compile_commands.json for Linux kernel driver development, enabling full code intelligence support with clangd.
- π Auto-extract CONFIG_ macros*: Extract all kernel configuration macros (947+) from
autoconf.h - π Smart file scanning: Automatically scan all compiled
.cfiles in the kernel - π Auto-generate compile commands: Generate from
.cmdfiles if kernel doesn't havecompile_commands.json - π οΈ Path fixing: Ensure clangd can correctly parse relative/absolute paths
- π Merge driver and kernel: Support generating compilation database for both driver code and kernel source
After generating compile_commands.json with this tool, clangd provides:
- β Code Completion: Auto-suggest functions, struct members, etc.
- π Go to Definition (F12): Jump to function/variable definitions
- π Find References: See where symbols are used
- π Hover Documentation: Show function prototypes and comments on hover
β οΈ Syntax Checking: Real-time code error diagnostics
In VSCode or Cursor:
- Press
Ctrl+Shift+Xto open Extensions - Search for "clangd"
- Install the "clangd" extension by LLVM
- Restart your editor
Note: If you have Microsoft C/C++ extension installed, consider disabling it to avoid conflicts.
sudo apt update
sudo apt install clangd
# Or install a specific version (clangd-15 or higher recommended)
sudo apt install clangd-15
sudo update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-15 100sudo dnf install clang-tools-extrasudo pacman -S clangbrew install llvm
# Add to PATH
export PATH="/usr/local/opt/llvm/bin:$PATH"Verify installation:
clangd --versionCreate a .clangd file in your project root:
CompileFlags:
Add:
# Ignore some GCC-specific warnings (not supported by clang)
- "-Wno-unknown-warning-option"
- "-Wno-unused-command-line-argument"
Remove:
# Remove GCC-specific options not supported by clang
- "-mno-thumb-interwork"
- "-fno-allow-store-data-races"
- "-fconserve-stack"
- "-mno-sched-prolog"
- "-mapcs"
- "-mabi=*"
Diagnostics:
# Suppress less important warnings
Suppress:
- "pp_including_mainfile_in_preamble"
- "-Wunused-variable"
InlayHints:
# Enable inline hints (show parameter names, types, etc.)
Enabled: true
ParameterNames: true
DeducedTypes: true
Hover:
ShowAKA: true # Show actual typedef typesPress Ctrl+Shift+P, type "Preferences: Open Settings (JSON)", and add:
{
// clangd configuration
"clangd.path": "/usr/bin/clangd",
"clangd.arguments": [
"--background-index",
"--clang-tidy",
"--completion-style=detailed",
"--header-insertion=iwyu",
"-j=4",
"--pch-storage=memory"
],
"clangd.fallbackFlags": [
"-std=gnu11"
],
// Disable Microsoft C/C++ IntelliSense (avoid conflicts)
"C_Cpp.intelliSenseEngine": "disabled",
// File associations
"files.associations": {
"*.h": "c",
"Kconfig*": "kconfig",
"Makefile*": "makefile"
}
}compile_commands.json (requires .cmd files and autoconf.h)
cd /path/to/kernel
# Set cross-compilation environment
export ARCH=arm
export CROSS_COMPILE=arm-none-linux-gnueabi-
# Generate default config
make defconfig
# Or use vendor config: make xxx_defconfig
# Or manual config: make menuconfig
# Build kernel (generates .cmd files and autoconf.h)
make -j$(nproc)export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make defconfig && make -j$(nproc)make defconfig && make -j$(nproc)# Generate for driver source only (fastest, recommended for daily use)
./clangd-kernel-helper
# Include kernel source (supports F12 jump to kernel function definitions)
./clangd-kernel-helper --with-kernel
# Force regenerate kernel's compile_commands.json
./clangd-kernel-helper --with-kernel --regenerate-kernel
# Use a different kernel directory
./clangd-kernel-helper --kernel-dir ~/linux-5.10 --with-kernel
# Specify cross-compiler and architecture
./clangd-kernel-helper --arch arm64 --cross-compile aarch64-linux-gnu-
# Show help
./clangd-kernel-helper --help| Option | Description |
|---|---|
--with-kernel |
Include kernel source compilation commands (supports jump to kernel implementation) |
--regenerate-kernel |
Force regenerate kernel's compile_commands.json |
--kernel-dir |
Specify kernel source directory (default: /home/sky/linux-4.14.139) |
--cross-compile |
Cross-compiler prefix (default: arm-none-linux-gnueabi-) |
--arch |
Target architecture (default: arm) |
# After pulling a new kernel, just specify the path
./clangd-kernel-helper --kernel-dir ~/linux-5.15 --with-kernel./clangd-kernel-helper \
--arch arm64 \
--cross-compile aarch64-linux-gnu- \
--with-kernel# Force regenerate after kernel config changes
./clangd-kernel-helper --with-kernel --regenerate-kernel- Ensure
compile_commands.jsonis generated - Check if paths in
compile_commands.jsonare correct - Restart clangd:
Ctrl+Shift+Pβ "clangd: Restart language server"
- Create
.clangdfile to remove clang-unsupported GCC options - Check if kernel is compiled (needs
autoconf.h)
- Use
--with-kerneloption to regenerate - Ensure kernel is compiled
- clangd builds index on first project open, please be patient
- Check indexing progress in VSCode status bar
- Index is cached after completion, subsequent opens will be fast
- Ensure
CONFIG_*macros are correctly extracted (check script output) - May need to run
--regenerate-kernelto regenerate
- Extract config macros: Extract all
CONFIG_*macro definitions fromautoconf.h - Scan source files: Traverse driver and kernel directories to find all
.cfiles - Generate compile commands: Generate compilation commands with correct header paths and macro definitions for each source file
- Fix paths: Convert relative paths to absolute paths to ensure clangd can parse correctly
- Merge output: Merge driver and kernel compilation commands into one
compile_commands.jsonfile
Contributions are welcome! Please see CONTRIBUTING.md for details.
- π Report bugs and issues
- π‘ Suggest new features
- π Improve documentation
- π§ Submit pull requests
- π Add translations for other languages
- β Star this project to help more people discover it
GPL-2.0 - See LICENSE file for details.
This project follows the same license as the Linux kernel to ensure compatibility.
If this project helps you, please consider giving it a star β to help more developers discover it!
Made with β€οΈ for kernel developers worldwide