.clangd: use different compiler flags depending on file extension

Viewed 407

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 ?

1 Answers

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.

Related