Hilberto1
Hilberto1

Reputation: 446

How can I change the version of the C++ language standard used in VS Code?

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?

Upvotes: 30

Views: 75690

Answers (6)

starball
starball

Reputation: 50024

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. Note that this is only for IntelliSense and does not affect the language standard used for the actual build.

If you're using CMake support from the CMake Tools extension ("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). Cache variables like CMAKE_CXX_STANDARD can be set in different ways: the cmake.configureArgs setting with -D, or in a configure preset via the cacheVariables property. I suggest using CMake Presets.

Also, if you're using a build task to do the compilation (for the cpptools with no CMake Tools setup, IntelliSense and build configuration are separate), make sure to add the corresponding compile flag to use that C++ standard (see also this post of mine). Not using the same language standard for build and IntelliSense is a recipe for confusion, errors, and possibly bugs. Ex. (in tasks.json):

{
    "type": "cppbuild",
    ...
    "args": [
        "-std=gnu++20",
        ...
    ],
    ...
}.

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 (I don't recommend this), 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).

Upvotes: 6

Alexander Dyagilev
Alexander Dyagilev

Reputation: 1425

You must use /Zc:__cplusplus command line switch for the __cplusplus macro to reflect the actual C++ standard version.

More details are here: https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170

Upvotes: 0

Syaiful Nizam Yahya
Syaiful Nizam Yahya

Reputation: 4305

If using g++, add "-std=gnu++20" under "args" in tasks.json file. Example

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-std=gnu++20",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Upvotes: 0

NoLifeKing
NoLifeKing

Reputation: 21

If you are using code runner, follow the following steps:

  1. Go to Settings -> extensions -> Run code configuration or you can just Click the gear icon of Code Runner in the extension tab and choose Extension Settings

  2. Scroll to the Code-runner: Executor Map settings then click Edit in settings.json

  3. You can see a line for cpp with the default "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"

  4. Change the line to "cpp": "cd $dir && g++ $fileName --std c++23 -fdiagnostics-color=always -o $fileNameWithoutExt && $dir$fileNameWithoutExt"

  5. The version that you will be using is specified in the after the flag --std, for example c++23

  6. Try running the code with Code Runner

Upvotes: 2

Thoth
Thoth

Reputation: 2276

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++20.

enter image description here

Upvotes: 47

Hassan
Hassan

Reputation: 159

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"
}

Upvotes: 15

Related Questions