Using C++17 or Later with the clangd Extension in VS Code

· updated · 2 min read

Recently I switched my code completion tool in VS Code from Microsoft’s official C/C++ extension to clangd. On the one hand, clangd gives a better experience; on the other hand, it is also what I use at my company (ByteDance).

I joked to myself that the two most interesting things I learned during my internship were that indentation went from four spaces to two, and that opening braces stopped getting a line of their own.

Now, there was a problem. I installed clangd, switched over, and everything worked fine, until I used std::string_view and clangd told me it didn’t recognize it. I quickly realized that clangd’s default C++ standard was still C++14, so changing that default became something I had to do.

I googled but didn’t find anything useful.

I tried a compile_commands.json file. But on the one hand, it has to be set up separately for each project, while what I wanted was C++17 for any file I happen to open. On the other hand, it didn’t work (maybe I didn’t configure it properly).

So here is the final solution. Open (create it if it does not exist) ~/.config/clangd/config.yaml and write:

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

Cool. Now let’s have fun with C++20.

Edit on 2023-10-21#

The path above is for Linux. On macOS, the configuration file is ~/Library/Preferences/clangd/config.yaml (on Windows it is %LocalAppData%\clangd\config.yaml). For details, see the clangd configuration docs.

You can also use If in config.yaml to avoid adding -std=c++2b to C files (which is funny):

If:
  PathExclude: [.*\.c, .*\.h]
CompileFlags:
  Add:
    - "-std=c++2b"

Excluding .h keeps C headers working, but C++ headers with a .h extension then fall back to the default standard. If your C++ code uses .h headers, exclude only .*\.c.


Revised in October 2026. Both configurations were checked with clangd --check (clangd 23). The C++14 default described above belongs to Clang 15 and earlier; since Clang 16 the default is gnu++17, so std::string_view works out of the box, but you still need this file for C++20 or C++23. Current compilers also accept -std=c++23 in place of -std=c++2b. The note on .h headers is new.