11

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?

starball
  • 20,030
  • 7
  • 43
  • 238
Hilberto1
  • 248
  • 1
  • 3
  • 10
  • 1
    You need to update your `tasks.json` and your `c_cpp_properties.json`. The `tasks.json` configures how your compiler builds your code. The `c_cpp_properties.json` configures intellisense in the editor. You also may need to upgrade your compiler. If you are on MS Windows and are using mingw, you may want to use the msys2 version which gives you the latest compiler and allows for easy install and upgrading and also has support for many libraries – drescherjm Mar 17 '21 at 19:43
  • 1
    @drescherjm and how do I have to update them? – Hilberto1 Mar 17 '21 at 19:46
  • The files are covered in the Microsoft Documentation provided you know your compiler command line switches: [https://code.visualstudio.com/docs/cpp/config-mingw](https://code.visualstudio.com/docs/cpp/config-mingw) – drescherjm Mar 17 '21 at 19:46
  • The tasks.json is used for the ctrl-B key. I don't know if it's used at all by the coderunner extension, since that has to build the command dynamically depending on the currently active source file. – JDługosz Mar 17 '21 at 22:54
  • You didn't say what platform you're on nor which compiler you're using. But, in a terminal window in VSCode, run the compiler command directly to see which compiler it found on the paths; e.g. `g++ --version`. Make sure the paths are set for the desired compiler, before running VSCODE. – JDługosz Mar 17 '21 at 22:58

3 Answers3

29

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.

enter image description here

Vendetta
  • 2,078
  • 3
  • 13
  • 31
  • 3
    If vscode intellisense still cannot detect the standard higher than C++14. Try to search in command palette (ctrl+p or ctrl+shift+p) with `C/C++: Edit Configurations (UI)`. Then check the `C++ standard` in this UI. I found it was set to `C++14` while `ms-vscode.cpptools` was set to `C++20`. – Louis Go Feb 02 '23 at 07:04
9

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"
}
dvhh
  • 4,724
  • 27
  • 33
Hassan
  • 99
  • 2
  • 7
1

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).

starball
  • 20,030
  • 7
  • 43
  • 238
  • Lots of options available depending on how you're using VSCode which provides lots of alternatives (because that's the nature of C++ development). Thanks for going over the list of them! – davidbak Jul 04 '23 at 15:03