Since I have struggled these warnings/errors for so long. I decided to ask this question and answer myself for late comers.
Extension: vscode-clangd
'auto' type specifier is a C++11 extensionclang(-Wc++11-extensions)
or any "grammar belong to C++11 or later" error. How to set default C/C++ standard in vscode-clangd?
Invalid argument '-std=c99' not allowed with 'Objective-C++'
or any header extension .h interpreted as C/C++ conflicting with your standard. How to deal with this annoying bugs?
Three ways
compile_flags.txt
:-std=c++11
complie_commands.json
by a build system like CMake, which can be enabled by adding this to your CMakeLists.txt file:set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
If no compilation database(compile_flags.txt
/complie_commands.json
) is found, you can do this in your vscode:
cmd/ctrl + shift + p
: Preferences: Open User Settings (JSON)settings.json
file:"clangd.fallbackFlags": [
"-std=c++11",
// do not add line below or it will cause conflict
// "-std=c99",
],
cmd/ctrl + shift + p
: clangd: Restart language serverYou just can't set C & C++ standard into your clangd.fallbackFlags
at the same time or it will conflict with each other. Two way to fix this:
-x
> man clang
-x <language>
Treat subsequent input files as having type language.
.clangd
into your project root with these lines:If:
PathMatch: .*\.cpp
CompileFlags:
Add: [-std=c++20]
---
If:
PathMatch: .*\.c
CompileFlags:
Add: [-std=c99]
clangd first check for a compilation database in the directory containing the source file, and then walk up into parent directories until we find one. The compilation database can be:
a file named compile_commands.json
listing commands for each file. Usually generated by a build system like CMake.
a file named compile_flags.txt
listing flags to be used for all files. Typically hand-authored for simple projects.
If no compilation database is found, use clangd.fallbackFlags
.