22

I'm trying to use a feature in c++17(const lambdas) without having clangd error me. I've searched online and every answer tells me to recompile clangd with a flag. Is there truly no other way?

Edit: Clangd is not the compiler. It's a language server, which is a program made to be used with IDEs that basically checks your code for errors and warnings before compiling it. See https://clangd.llvm.org .

5
  • 3
    clangd.llvm.org/config#compileflags Commented Sep 17, 2022 at 21:08
  • 2
    @n.1.8e9-where's-my-sharem. Read my question again, I believe it clearly states that I was asking for a solution that didn't require me to recompile clangd. Commented Sep 17, 2022 at 21:17
  • 2
    @AaronLiu did you read his link? It’s the correct answer to your question. Clangd accepts compiler flags to determine how to interpret the files. Commented Sep 17, 2022 at 23:07
  • See this answer to a similar question. No recompiling clangd required, just creating a config file in the root directory of your project. Commented Sep 18, 2022 at 4:22
  • I read it but misinterpreted it. Sorry. Commented Sep 18, 2022 at 15:28

2 Answers 2

35

Answer inspired by n. 1.8e9-where's-my-share m.

From https://clangd.llvm.org/config#compileflags:

You can create a config file for clangd. In the config file, you can specify the compiler options mimicked. For my question, do this:

CompileFlags:
  Add: [-std=c++20]

Put the configuration file as .clangd in a shared parent directory of the files you want this to apply to. If you want it to apply for the entire user, make it a config.yaml file in an OS-specific directory:

  • Windows: %LocalAppData%\clangd\config.yaml, typically C:\Users\Bob\AppData\Local\clangd\config.yaml.
  • macOS: ~/Library/Preferences/clangd/config.yaml
  • Linux and others: $XDG_CONFIG_HOME/clangd/config.yaml, typically ~/.config/clangd/config.yaml.
Sign up to request clarification or add additional context in comments.

5 Comments

The config file is named ~/.clangd. (ref: Configuration Files)
This worked for me using the clangd Visual Studio Code extension as it just uses the machine's installation of clangd under the hood.
Unfortunately, this will also force c files to be treated as c++20 files, which is probably not desirable.
Is it possible to set the C++ version per project?
@FelixDombek @ynn’s path only works if your project file is under any ~/ or any subdirectory of it, as they’ve provided the way to specify it “per-project” for all subdirectories of ~/. I’ve edited the answer.
1

As of clangd12, clangd can use a compilation database (compile_commands.json) just like clang-tidy. You can avoid hard-coding compilation flags (including language standard) if your build tool supports compilation databases.

A compilation database is an autogenerated file that contains a precise dump of compiler settings. They are used to communicate between various C++ tools.

Example with CMake 3.5+ and default clangd config

Use project source directory or a build/ subdirectory as the binary directory and enable the CMAKE_EXPORT_COMPILE_COMMANDS flag:

cmake -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
# or cmake -B . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

clangd will now use the same settings as the compiler would.

Search paths

By default, when processing any C/C++ file, clangd searches for a compile_commands.json in the file's directory and all parent directories, and in the build/ subdirectory in each of those directories, as pointed out in this answer.

This behavior can be changed via clangd configuration.

3 Comments

Thanks. I get what you mean, but could you add an example compilation database that compiles C++ files with the -std=c++20 flag?
I could add an example to the answer itself, but compilation databases are extremely machine-, project- and configuration-specific. It shouldn't really matter because they are autogenerated by build tools. If you don't use tools that could autogenerate one for you, then use your previous answer. Compilation databases are a standardized way to communicate between automatic tools, maintaining them by hand is nearly impossible. For example, they contain absolute paths.
Note that you are most likely already using a tool that can export compilation databases, at least via extensions: CMake, Bazel, most IDEs, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.