Search code examples
c++cmakeneovimclangd

nvim + clangd | compile_commands.json with MetaProject


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!


Solution

  • 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()