3

I am using the .clangd configuration file to pass compilation flags to clangd.

I run clangd on a codebase with C and C++ files.

How can I have some flags apply to C++ files but not C files ?

Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53
  • 1
    If your build system supports automatic generation of `compile_commands.json`, you should use it instead of manually configuring flags. – HolyBlackCat Mar 14 '22 at 18:12
  • My build system is b2 which sadly not support `compile_commands.json` yet. I plan to try https://github.com/tee3/commands_to_compilation_database but I wanted to better understand `.clangd` possibilities first. – Gabriel Devillers Mar 14 '22 at 18:14

1 Answers1

3

You can use multiple fragments and the If block syntax.

Example:

# Fragment common to C and C++ source files
CompileFlags:
    Add:
        - "--include-directory=some/directory/to/search/includes/in"
        - "-D"
        - "SOME_MACRO_TO_DEFINE"
        - "-include"
        - "some/file/to/force/inclusion/of.h"

---
# Fragment specific to C++ source files
If:
    PathExclude: [.*\.c, .*\.h]
CompileFlags:
    Add:
        - "-std=c++17"

--- delimits fragments.

PathMatch could be more practical than PathExclude depending on the extensions in your codebase.

I tested this on clangd 13.0.0.

I got help from this Github issue.

More help in clangd official documentation.

Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53