Small C++ project using CMake, set to c++20, compiles fine using <string_view>
.
- host OS: Windows 10, remote OS: Ubuntu 20.04
- VSCode versions: VSCode 1.71.2, C/C++ extension 1.12.4, CMake Tools 1.12.27
- compiler: clang 15 as installed by
apt.llvm.org/llvm.sh
But VSCode IntelliSense insists that std::string_view
is undefined:
If I F12
on #include <string_view>
I go to /usr/include/c++/9/string_view
where I see that it thinks the built-in preprocessor macro __cplusplus
is set to c++11:
It is getting its configuration from CMake Tools as seen by the following message in the "C/C++ Configuration Warnings" output window:
[10/3/2022, 9:57:47 PM] For C++ source files, the cppStandard was changed from "c++23" to "c++20" based on compiler args and querying compilerPath: "/usr/bin/clang"
(I had set the VSCode IntelliSense Configuration "C++ standard" to "c++23" while my CMake project was still saying "c++20".)
My c_cpp_properties.json
:
{
"configurations": [
{
"name": "Linux",
"intelliSenseMode": "linux-clang-x64",
"includePath": [],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++20",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
Why is the built-in __cplusplus
macro wrong for whatever it is that IntelliSense is doing?
The IntelliSense engine is "default":
I installed the extension clangd
--Clang's own language server--and disabled the built-in IntellSense (clangd
warned of a conflict there, which would be expected). This doesn't work either, but now __cplusplus
is set to c++14--no other change to any settings were made.
I did try the solution suggested in this answer to How to change C++ version being used by VS Code? which is to set the language in the C/C++ extension settings. As is demonstrated by the c_cpp_properties.json
file included--they are the "cStandard" and "cppStandard" properties. And the question also shows that those properties aren't used in this case because I'm using the setting where IntelliSense asks the build system--CMake Tools in this case--for the proper settings: that is shown by the actual message given by CMake Tools.