blog/src/content/posts/2022-03-03-clangd-in-nix.md
Michael Zhang 4a89b35ba3
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
ok kinda working
2023-08-31 03:07:03 -05:00

1.1 KiB

title date tags
Clangd in Nix 2022-03-03
nixos

I've been using Nix a lot recently since it handles dependency management very cleanly, but one gripe that I've been having is that when I'm doing C/C++ development work using nix develop, all my dependencies are actually in the Nix store in /nix, so my clangd editor plugin won't be able to find them.

Fortunately, clangd supports looking for a file called compile_commands.json, which describes the compilation commands used for each file, with absolute paths for all dependencies.

For CMake-based projects, there's an option to dump this information automatically into the build directory, which I typically then symlink into my project's root directory for my editor to find and apply to my files. Here's the snippet:

# Generate the `compile_commands.json` file.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")

if(CMAKE_EXPORT_COMPILE_COMMANDS)
  set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES
    ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
endif()