Search code examples
visual-studio-codec++20vscode-tasks

configure C++20 in VSCode


I tried to set vscode with C++20, but I did not succeed. I admire your helps.

here is my programme. It includes C++20 specific commands. Also the last command gives the version of C++ it is using.

//Main.cpp

#include <iostream>
#include <ranges>
#include <algorithm>
#include <array>
namespace rg = std::ranges;

int main()
{
    auto arr = std::to_array({12, 8, 3, 7});

    rg::sort(arr);
    for (size_t i = 0; i < arr.size(); ++i)
    {
        if (i != 0)
            std::cout << ", ";
        std::cout << arr[i];
    }
    std::cout << "\n\n"
              << __cplusplus; //this will return the version of C++
}

I used g++ Main.cpp -std=c++20 && .\a in CMD and simply got the correct answer which is:

3, 7, 8, 12

202002

but in vscode I can't set the version of C++ from 17 to C++20. Here is the errors I encounterd in vscode:

if ($?) { g++ 1.cpp -o 1 } ; if ($?) { .\1 }
1.cpp:5:21: error: 'ranges' is not a namespace-name
    5 | namespace rg = std::ranges;
      |                     ^~~~~~
1.cpp: In function 'int main()':
1.cpp:9:21: error: 'to_array' is not a member of 'std'
    9 |     auto arr = std::to_array({12, 8, 3, 7});
      |                     ^~~~~~~~
1.cpp:9:21: note: 'std::to_array' is only available from C++20 onwards
1.cpp:11:5: error: 'rg' has not been declared
   11 |     rg::sort(arr);
      |     ^~

Here is my tasks.json:

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

Here is settings.json:

{
    "C_Cpp.default.cppStandard": "c++20",
    "C_Cpp.default.cStandard": "c17",
    "C_Cpp.default.compilerPath": "C:\\msys64\\ucrt64\\bin\\gcc.exe",
    "files.associations": {
        "array": "cpp",
        "atomic": "cpp",
        "bit": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "compare": "cpp",
        "concepts": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "string": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "numeric": "cpp",
        "optional": "cpp",
        "random": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "numbers": "cpp",
        "ostream": "cpp",
        "ranges": "cpp",
        "span": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "typeinfo": "cpp",
        "variant": "cpp"
    }
}

This is launch.json:

{
    
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++: g++.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys64\\ucrt64\\bin\\gdb.exe",
            "setupCommands": [
              {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
              },
              {
                "description": "Set Disassembly Flavor to Intel",
                "text": "-gdb-set disassembly-flavor intel",
                "ignoreFailures": true
              }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
          }
    ]
}

And this is c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "GCC",
            "includePath": ["${workspaceFolder}/**"],
            "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
            "windowsSdkVersion": "10.0.22000.0",
            "compilerPath": "C:/msys64/ucrt64/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "windows-gcc-x64"
          }
    ],
    "version": 4
}

additionally, my settings work with programs that don't need C++20 and use just C++17


Solution

  • I added this to settings.json and it is now working with C++20

    "code-runner.executorMap": {
        "cpp": "cd $dir && g++ -std=c++20 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
    }