1

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.

starball
  • 20,030
  • 7
  • 43
  • 238
jiandingzhe
  • 1,881
  • 15
  • 35

1 Answers1

3

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

starball
  • 20,030
  • 7
  • 43
  • 238