See the VS Code Clangd extension's Project Setup docs, which state:
you must tell clangd how your project is built (compile flags). A compile_commands.json file can usually be generated by your build system (e.g. with CMake, by setting -DCMAKE_EXPORT_COMPILE_COMMANDS=1
).
See Project Setup in the clangd documentation for details and alternatives.
In the linked clangd docs, you'll see:
compile_commands.json
This file provides compile commands for every source file in a project. It is usually generated by tools.
clangd will look in the parent directories of the files you edit looking for it, and also in subdirectories named build/
. For example, if editing $SRC/gui/window.cpp
, we search in $SRC/gui/
, $SRC/gui/build/
, $SRC/
, $SRC/build/
, …
Since CMake generates the compile commands database in root of the build tree, unless you're doing an in-source build, you'll probably need to either
See also the docs for CMAKE_EXPORT_COMPILE_COMMANDS
(note that it's only supported at the time of this writing if you're using a Ninja or Makefiles generator). You'll probably want to add that copy's path to your .gitignore.
Note for other readers who may be using the CMake Tools extension (unlike the asker here): The docs say to use -D...
, which you do if you're calling the configuration command yourself via commandline. If you're doing it through the VS Code CMake Tools extension, you can either use the cmake.configureSettings
setting it contributes, or write a CMake configure preset and use the cacheVariables
property (since CMake Tools supports CMake presets).