Skip to content

Instantly share code, notes, and snippets.

@pohzipohzi
Last active February 8, 2024 08:23
Show Gist options
  • Save pohzipohzi/cebc720c3e9cd809adc20b7f3424d7d8 to your computer and use it in GitHub Desktop.
Save pohzipohzi/cebc720c3e9cd809adc20b7f3424d7d8 to your computer and use it in GitHub Desktop.
setting up clangd

Here are some examples of how to setup clangd for a project

If the project:

  • uses meson

meson setup <build_dir> generates a compilation database in <build_dir>/compile_commands.json. Since clangd automatically looks for the compilation database in build, we don't need to do anything else if build is the name of the build directory

meson setup build

If we want to use a custom build directory (eg. meson setup builddir), we can add a new file .clangd to the root of the directory to specify the path to the compilation database

CompileFlags:
  CompilationDatabase: builddir

Or just symlink the compilation database to the root of the directory

ln -s builddir/compile_commands.json .
  • uses ninja

ninja -t compdb > compile_commands.json
  • uses cmake

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1
  • uses make

Use bear

bear -- make
  • uses something else

If the project does not use any of the above (eg. it is a rust sys crate containing some header files), we can still setup clangd manually using a configuration file. This can be done per project (/path/to/project/.clangd) or per OS user (~/.config/clangd/config.yaml)

CompileFlags:
  Add:
    - -I/usr/include/lib1
    - -I/usr/include/lib2
    - ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment