I printed the value of the __cplusplus
macro and found out that my files are executed with C++98 in Visual Studio Code. I'm using the CodeRunner extension.
How do I change this to C++17?
I printed the value of the __cplusplus
macro and found out that my files are executed with C++98 in Visual Studio Code. I'm using the CodeRunner extension.
How do I change this to C++17?
Go to extensions, then type ms-vscode.cpptools
in the search bar.
Click on the C/C++
extension, and to the right of Uninstall
, there should be a gear icon. Click it.
A dropdown menu should open. Select Extension Settings
.
Now click in the search bar (sometimes it makes you click twice before you can type without replacing the extension filter) and type cppStandard
.
From here, you should see two options, one for Cpp Standard, and one for C Standard.
Change Cpp Standard to your desired version. I generally use c++17.
Also, make sure your debugger is using the same version. In task.json the line after --std
defines the version.
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"--std",
"c++20",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
If you're using the cpptools extension without CMake support, then use the C_Cpp.default.cppStandard
setting, or the corresponding property of a specific configuration in your c_cpp_properties.json
.
If you're using CMake support ("configurationProvider": "ms-vscode.cmake-tools"
), then adjust your CMake configuration in whichever way is the most suitable for your project (Ex. target_compile_features
, CMAKE_CXX_STANDARD
).
Also, if you're using a build task to do the compilation, make sure to add the right compile flag to use that C++ standard (see also this post of mine).
If you're using the clangd extension, that goes off of a compile commands database (compile_commands.json
file), which you can get automatically from CMake using CMAKE_EXPORT_COMPILE_COMMANDS
(see also this post of mine).
If you're using VS Code's Code Runner extension, see How can I change the C++ standard used by the Code Runner extension for VS Code? (edit the workspace .vscode/settings.json's code-runner.executorMap
setting).