1

I am setting up nvim for my work project which is built using empty MetaProject that builds multiple sub-projects and I have issue with clangd. It doesn't recognize custom headers or classes. I know that compile_commands.json is needed for clang in order to know where to look, but when I build project with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON I won't have that file generated in root directory, but multiple files in multiple sub-projects.

I found compile_commands.json for my project but how can I can tell clangd this custom path in build directory?

Let's say that path is ${workspaceFolder}/MetaProject/build/1/2/3/compile_commands.json

I am new to nvim and i use NvChad + additionally installed clangd.

I would like to kindly ask you for help because I don't know how to proceed and working without clangd is a pain in the ***. Cheers!

Morph
  • 165
  • 2
  • 9

1 Answers1

3

how can I can tell clangd this custom path in build directory?

From https://clangd.llvm.org/config#compilationdatabase

# .clangd
CompileFlags:
   CompilationDatabase: the path

As I work only on Linux, I found it is extremely simpler to just create a symlink from project dir to the build directory. Like so in root CMakeLists.txt

if (THE_PROJECT_DEV)  # a flag to check for project developers
    execute_process(
        COMMAND ${CMAKE_COMMAND} -E create_symlink
            ${CMAKE_BINARY_DIR}/compile_commands.json
            ${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json
    )
endif()
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • this would make a good addition to https://stackoverflow.com/q/57464766/11107541 – starball May 25 '23 at 19:30
  • One additional possibility is to use the `--compile-commands-dir` command-line argument to clangd to tell it where to look for `compile_commands.json`. This has the advantage that it works even for headers outside the directory containing the `.clangd` file (see https://clangd.llvm.org/faq#how-do-i-fix-errors-i-get-when-opening-headers-outside-of-my-project-directory). – HighCommander4 May 25 '23 at 21:31