如何为 C++ 14 /C ++17 设置 VS 代码

How to Setup VS Code For C++ 14 /C ++17

我试图从工作区 运行 一个 .cpp 文件,但给我这个关于不添加 c++11/更高标志的错误,但我已经在 task.json

中添加了它们

错误

[Running] cd "c:\Users\Nuhash\Desktop\test\" && g++ main.cpp -o main && "c:\Users\Nuhash\Desktop\test\"main
main.cpp:8:1: error: expected unqualified-id before 'using'
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
^
main.cpp:10:1: error: expected unqualified-id before 'using'
using ordered_set_rev = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
^
main.cpp:12:1: error: expected unqualified-id before 'using'
using dijkstra = priority_queue<T, vector<T>, greater<T>>;
^
main.cpp:62:31: warning: variadic templates only available with -std=c++11 or -std=gnu++11
template <typename T, typename... Args>
                               ^
main.cpp:63:52: warning: variadic templates only available with -std=c++11 or -std=gnu++11
void err(istream_iterator<string> it, T a, Args... args) {


Task.Json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-o",
                "test",
                "-std=c++14",
                "main.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

错误信息:

[Running] cd "c:\Users\Nuhash\Desktop\test\" && g++ main.cpp -o main && "c:\Users\Nuhash\Desktop\test\"main
main.cpp:8:1: error: expected unqualified-id before 'using'
 using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 ^
main.cpp:10:1: error: expected unqualified-id before 'using'
 using ordered_set_rev = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
 ^
main.cpp:12:1: error: expected unqualified-id before 'using'
 using dijkstra = priority_queue<T, vector<T>, greater<T>>;
 ^
main.cpp:62:31: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <typename T, typename... Args>
                               ^
main.cpp:63:52: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 void err(istream_iterator<string> it, T a, Args... args) {

c_cpp_properties:

    {

        "name": "Win32",
        "includePath": [
            "${workspaceFolder}"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "intelliSenseMode": "clang-x64",
        "browse": {
            "path": [
                "${workspaceFolder}"
            ],
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": ""
        },
        "compilerPath": "F:\Program Files (x86)\CodeBlocks\MinGW\bin\gcc.exe",
        "cStandard": "c11",
        "cppStandard": "c++17"
    }

cppStandard分别设置为c++17c++14

你需要那个 https://github.com/Microsoft/vscode-cpptools

的 C++ 扩展

我添加了代码运行器并在 settings.json 中添加了它似乎对我有用:D

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

"-std=c++11", 添加到 task.json 文件中的 "args" 值。那应该可以解决您的 c++11 问题。 所以最后你的 task.json 看起来像

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",                
                "-std=c++11",
                "-std=c++14",
                "main.cpp",
                "-o",
                "test"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

我没有在 c_cpp_properties.json 中添加 MinGW 头文件。 工作正常后 c_cpp_properties.json 看起来像这样......

{
"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**",
            "C:\Program Files (x86)\CodeBlocks\MinGW\include"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "compilerPath": "\"C:\Program Files (x86)\CodeBlocks\MinGW\bin\g++.exe\"",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "gcc-x86"
    }
],
"version": 4
}

并将我的 tasks.json 更改为...

{
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "g++.exe build active file",
        "command": "g++.exe -std=c++14 -g ${file} -o ${fileDirname}\${fileBasenameNoExtension}.exe && ${fileDirname}\${fileBasenameNoExtension}.exe",
        "options": {
            "cwd": "C:\Program Files (x86)\CodeBlocks\MinGW\bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build"
    }
]
}

现在构建并运行编译后的文件

  • 打开 VS 代码
  • 转到扩展程序 (Ctrl +Shift + X)
  • 然后只需点击代码 运行ner
  • 然后点击那里的设置图标
  • 然后选择扩展设置
  • Photo
  • 设置将打开,

  • 点击Code-Runner-Executer Map下settings.json中的编辑

  • Photo

  • 在json文件中找到“cpp”,在&& g++
  • 后面加上-std=c++17
  • 最后它看起来像这样 "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
  • 保存,大功告成]
  • Photo
  • 如果此代码 运行s 没有任何错误您已启用 C++17,请尝试此代码 Link
  • 确保当你运行代码终端看起来像这样
  • Photo
  • 注意:如果你不使用code Runner我会建议你这样做
  • 如果您使用 windows 并且您的编译器不支持 c++17
  • 您可以从下面安装MinGw最新编译器(9.2.0)link
  • Link