Clangd Auto Config Extension
A VSCode extension that automatically generates compile_commands.json and .clangd configuration files for C/C++ projects.
Features
- Automatic Generation: One-click generation of
compile_commands.json using compiledb command
- Configurable: Customize the compilation database generation command
- Smart Configuration: Load global variables from
.config file and add them to .clangd as compiler defines
- Customizable Template: Modify the default
.clangd configuration template in VSCode settings
- Background Indexing: Optimized for performance with background indexing and multiple workers
- Real-time Monitoring: Auto-update
.clangd configuration when .config or config.* files change
- Conditional Branch Support: Correctly parse Makefile-style conditional branches in configuration files
- Variable Resolution: Resolve Makefile-style variable references (e.g.,
$(VAR_NAME))
- Duplicate Macro Prevention: Avoid duplicate macro definitions in generated configuration
- Webview Sidebar: Real-time status display and quick access to functionality
Requirements
- VSCode 1.60.0 or higher
compiledb tool installed (pip install compiledb)
Installation
- Clone or download this extension
- Open VSCode and go to Extensions view
- Click "Install from VSIX..." and select the extension package
- Or run
npm install and vsce package to build the extension
Usage
- Open your C/C++ project in VSCode
- Press
Ctrl+Shift+P to open the Command Palette
- Type "Generate Clangd Configuration" and select the command
- The extension will:
- Generate
compile_commands.json using the configured command
- Load global variables from
.config file (if exists)
- Generate
.clangd configuration file with merged settings
Configuration
You can customize the extension behavior in VSCode settings:
clangd-autoconfig.compiledbCommand
Command to generate compile_commands.json. Default: compiledb -n make
clangd-autoconfig.defaultClangdConfig
Default .clangd configuration template. Modify this to change the default settings.
Example:
{
"clangd-autoconfig.defaultClangdConfig": {
"CompileFlags": {
"InheritFromEnvironment": false,
"Add": ["-std=c++17"]
},
"Index": {
"Background": "Build",
"BackgroundWorkers": 8
},
"General": {
"MemoryUsage": "1000MB"
}
}
}
How It Works
- Generate compile_commands.json: Executes the configured
compiledb command in the workspace root
- Load .config Variables: Parses
.config file for variable definitions (e.g., VARIABLE=value)
- Generate .clangd: Creates a YAML configuration file with:
- Default settings from the template
- Global variables from
.config as -D compiler defines
Troubleshooting
- compiledb not found: Install it using
pip install compiledb
- Permission denied: Ensure VSCode has write access to the workspace directory
- Config file not loaded: Check that
.config exists in the workspace root with valid variable definitions
License
MIT
| |