Navigate C and C++ Code in Any Editor With clangd
Set up clangd for go-to-definition, completion and live diagnostics in VS Code, Neovim or any editor, using a compile_commands.json from your build.

Reading a large C or C++ project in a plain text editor means grepping for names and guessing which of five functions called init you are looking at. A language server fixes that: it parses the code the way the compiler does, so "go to definition" and "find all references" are exact. clangd is the language server built on Clang, and it works with any editor that speaks the Language Server Protocol, which is nearly all of them. This post sets it up from scratch with Clang 18: installing it, giving it the compile flags it needs, wiring it into VS Code and Neovim, and fixing the three problems that cause nearly every "clangd does not work" report.
What clangd gives you
Once it is running, an editor can offer completion that knows types, jumps from a call to the function's definition or declaration, lists every use of a symbol across the project, renames a symbol safely, shows compiler errors and warnings as you type, offers quick fixes (the same suggestions Clang prints on the command line), and adds missing #include lines. It also runs clang-tidy checks inline when you ask it to and formats code with clang-format.
All of this depends on one fact: clangd has to compile each file the way your build does. It needs the include paths, the defines and the language standard. That information comes from a compilation database.
Install
clangd is a separate executable, normally packaged with Clang. On Debian and Ubuntu, sudo apt install clangd; on Fedora, sudo dnf install clang-tools-extra; on macOS, brew install llvm gives it, and the Xcode command line tools also include a copy. VS Code has an extension that downloads it for you. Check that it runs:
clangd --versionclangd version 18.1.8
Features: linux+grpc
Platform: x86_64-pc-linux-gnuThe feature list depends on how your build was configured.
Give it a compilation database
The compilation database is a file named compile_commands.json that lists, for every source file, the directory and command used to compile it. clangd looks for it in the project's root directory and in a build directory below the file it is analyzing. How you get one depends on the build system, and each of these is covered in more detail in a companion post.
CMake: configure with
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON, and the file appears in the build directory.Meson: it writes
compile_commands.jsoninto the build directory automatically.Ninja:
ninja -t compdb > compile_commands.jsonextracts it from an existing build.Make and other build systems: wrap the build with Bear,
bear -- make, which records every compiler call.
A typical CMake setup puts the build in build/ and links the database to the project root:
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
ln -sf build/compile_commands.json compile_commands.jsonAn entry in the file looks like this, abbreviated:
{
"directory": "/home/dev/app/build",
"command": "/usr/bin/cc -I/home/dev/app/include -O2 -std=gnu17 -o CMakeFiles/app.dir/main.c.o -c /home/dev/app/main.c",
"file": "/home/dev/app/main.c"
}Without a database, clangd falls back on guessing: it assumes the file is a standalone program with default flags, can not find your headers and shows errors on every #include for a project header.
For a small project with no build system, a compile_flags.txt file in the root, with one flag per line (-Iinclude, -std=c11, -DDEBUG), does the job for all files.
Connect it to your editor
VS Code. Install the "clangd" extension from the LLVM project (publisher llvm-vs-code-extensions). If you also have Microsoft's C/C++ extension installed, disable its IntelliSense engine for this workspace, because two language servers produce duplicate and conflicting results. The setting is "C_Cpp.intelliSenseEngine": "disabled". The clangd extension offers to download the server on first use.
Neovim. With nvim-lspconfig, one line starts it:
require('lspconfig').clangd.setup({
cmd = { 'clangd', '--background-index', '--clang-tidy' },
})Neovim's built-in LSP client supplies the keybindings you configure for definition, references and rename. In Neovim 0.11 and later, vim.lsp.config and vim.lsp.enable('clangd') is the newer interface.
Emacs, Helix, Sublime, Vim and others. Each has a language-server client (eglot or lsp-mode for Emacs, built-in for Helix, the LSP package for Sublime Text, vim-lsp or coc.nvim for Vim). Configure the server command as clangd, and the same features appear.
CLion and other IDEs have their own analysis engines, and some can use clangd as an option.
Useful command-line flags
Pass these in the editor's server configuration:
--background-indexbuilds a project-wide index in the background so that find-references and workspace symbol search cover files you have not opened. The index is cached on disk and updated as files change.--clang-tidyruns the checks from your.clang-tidyfile and shows them as diagnostics.--header-insertion=neverstops it from adding#includelines automatically, if you find that intrusive. The default isiwyu.--completion-style=detailedshows more signature detail in completions.-j=4limits the number of parallel threads, which matters on a laptop.--query-driver=/usr/bin/arm-none-eabi-*letsclangdask a named cross-compiler for its system include paths, which solves "stddef.h not found" for embedded toolchains.--compile-commands-dir=buildpoints at a database in a non-standard place.
Configure per project with .clangd
A .clangd file in the project root adjusts flags and diagnostics without touching the build. It is YAML:
CompileFlags:
Add: [-Wall, -Wextra]
Remove: [-mcpu=*, -mthumb]
Diagnostics:
UnusedIncludes: Strict
ClangTidy:
Add: [bugprone-*, performance-*]
Remove: [bugprone-easily-swappable-parameters]The Remove list helps with cross-compiled projects, where the database contains flags that the host clangd rejects, such as -mcpu=cortex-m4. You can have different sections for different paths using If: PathMatch:.
Diagnose problems
Three problems account for almost every complaint.
Errors on every project header, red squiggles under includes. clangd is not finding the compilation database. Check that compile_commands.json exists at the project root or in build/, and open the file from the project root as the workspace. Run the checker from the command line, which prints exactly what clangd sees:
clangd --check=src/main.cI[10:42:11.512] Loaded compilation database from /home/dev/app/compile_commands.json
I[10:42:11.540] Parsing command...
I[10:42:11.540] Parsing /home/dev/app/src/main.c
I[10:42:11.790] Built preamble of size 14223 for file /home/dev/app/src/main.c
All checks completed, 0 errorsThe timestamps and sizes are examples. The line to look for is the first one: which database was loaded.
Errors about unknown flags or missing standard headers with a cross-compiler. The database holds flags for a compiler that clangd cannot find system headers for. Use --query-driver and remove host-incompatible flags in .clangd.
Stale or wrong results after a build change. Regenerate the database, since it records the commands as they were when you made it, and restart the server. In VS Code, run the command "clangd: Restart language server". The cache in .cache/clangd can be deleted safely.
Files that are not in the database at all, such as headers that no source includes yet, get the flags of a sibling file in the same directory. A new header opened on its own may therefore show errors until a source file includes it.
A realistic first session
Open a project, wait for the status bar to say indexing is complete, and put the cursor on a function name. Go to definition jumps to the body even if it is in another library's header. Find references lists every caller. Hover shows the signature and the doc comment. Rename changes every use, including those in other files, and shows a preview. Type a call with a wrong argument and the editor underlines it with the same message clang would print. That is the baseline, and it works on a project of any size, because the index is incremental.


