2

I have a C++ project consisting of multiple source and header files, and a Makefile to steer the build process. Building is done using clang++. Because I am not satisfied with the performance of the native C/C++ extension of VS code (although the codebase is quite small; ~5k lines, I cannot get completions instantaneously), I installed the clangd extension and set up a compile_commands.json file. Completion, linting etc. works nicely, but I have constant difficulties jumping to symbols in other files:

  • Jumping to symbol does not work if the file is not opened in VS Code;
  • go to definition/implementation/declaration often jumps to a wrong spot, or does nothing, expecially when used on members of classes.
  • symbols from other files often are not available for autocompletion.

How to I setup clangd correctly for my project?

My compile_commands.json reads

[
  {
    "directory": "/.../",
    "command": "clang++ -I/usr/local/include -I/usr/local/opt/libomp/include -std=c++2a -MT release/source.o -MMD -MP -MF release/source.d Wall -O3 -fopenmp -flto -DNDEBUG -c -o release/source.o source.cpp",
    "file": "source.cpp"
  },
  ...
]

for every source file in the project. Clangd reports no errors in the code, so it seems to read the compile commands.

I am using Homebrew clang++ and clangd version 15.0.7.

Bubaya
  • 615
  • 3
  • 13
  • You need `compile_commands.json` with a list of source files. If you already tried it, can you provide more details on what you did? – HolyBlackCat Mar 04 '23 at 11:02
  • @HolyBlackCat Done; does not solve the problem. – Bubaya Mar 04 '23 at 13:53
  • 1
    Ok, we'll need a [mcve] then. Let's say two .cpp files, one function per file. Add the exact content of those, plus `compile_commands.json`, the completions you get, etc. – HolyBlackCat Mar 04 '23 at 13:58
  • If you would use CMake, type the command `cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=1` from your source root. This will instruct CMake to build a compile_commands.json in the /build folder, which clang should find automatically. – dirk Mar 04 '23 at 15:38
  • @dirk note that `CMAKE_EXPORT_COMPILE_COMMANDS` is only supported for specific generators. From [the docs](https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html): "_Note This option is implemented only by Makefile Generators and Ninja Generators. It is ignored on other generators._" – starball Mar 04 '23 at 16:59
  • @dirk The problem is not generating the `compile_commands.json`-file. As linting reports no errors, it seems the file is properly read by clangd. – Bubaya Mar 04 '23 at 17:06
  • DId you create the `compile_commands.json` manually, or using a tool (and if so which tool)? If you're using `make` to build, I recommend using [Bear](https://github.com/rizsotto/Bear) (and then `make clean` followed by `bear -- make` should generate one). I do not recommend authoring the file manually as it can be finnicky to get right. – HighCommander4 Mar 06 '23 at 06:26
  • @HighCommander4 I used Bear. – Bubaya Mar 06 '23 at 12:16

0 Answers0