Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make the VS Code clangd extension aware of the include paths defined in my CMake configuration?

I’m coding C++ on a Raspberry Pi 3B via VSCode’s remote ssh. Currently, I just manually add all extra include directories (and other compiler settings) in settings.json:

{
    "clangd.fallbackFlags": [
        "-I/usr/include/foo",
        "-I/usr/include/bar",
        "-I${workspaceFolder}/src",
        "-std=c++17"
    ]
}

And I’m seeking for an automated way to do that.

I’m NOT using the CMake Tools and IntelliSense plugin, as it is very CPU and memory consuming, almost exhaust RPi’s 1GB memory. In contrast, clangd is much lighter and capable by a RPi.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 copy the file manually to your source tree. See also Copy compile_commands.json to project root folder.

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading