如何设置VS Code用于C++14 / C++17

12
我尝试从工作区运行一个.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"
    }

3
你确定那是正确的 tasks.json 文件吗?它和错误信息不匹配。 - Alan Birtles
是的,它之前一直运行良好。然后我重新安装了Windows和VS Code,但由于某种原因它现在无法工作。Task.json一开始就在文件夹中... - sabertooth
我同意Alan的评论。构建输出[Running] cd "c:\Users\Nuhash\Desktop\test\" && g++ main.cpp -o main && "c:\Users\Nuhash\Desktop\test\"main与您的tasks.json不匹配。 - pooya13
是的,但是 tasks.json 是正确的。不知道为什么命令与 tasks.json 不匹配,我已经重新创建了几次。无论如何,我通过更改 settings.json 中的代码运行器配置来修复了它。 - sabertooth
这个回答解决了你的问题吗?如何在VS Code中更改C++语言标准的版本? - undefined
5个回答

24

我添加了Code Runner,并在settings.json中添加了这个。对我来说似乎是有效的 :D

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

1
代码运行器是一个功能非常有限的扩展,甚至对于最基本的C++开发也没有什么用处。不要使用它。 - n. m.
@n.m. 的问题是关于运行单个 .cpp 文件的。在大多数情况下,代码运行器非常有用,可以用来运行单个文件。 - sabertooth

5
在您的task.json文件中,将"args"值添加"-std=c++11",。这样应该解决您的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"
            ]
        }
    ]
}

1
@uma_8331 你可以按Ctrl+Shift+P,然后选择“任务:配置任务”,VSCode会为你创建一个任务。 - MHebes

4
  • 打开 VS Code
  • 进入扩展 (Ctrl+Shift+X)
  • 然后直接点击代码运行器
  • 然后在那里单击设置图标
  • 然后选择扩展设置
  • 图片
  • 设置将会打开

  • 在Code-Runner-Executer Map下单击编辑settings.json

  • 图片

  • 在 json 文件中查找 "cpp" 并在 "&& g++" 后添加 -std=c++17
  • 最终它看起来像这样 "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
  • 保存即可完成
  • 图片
  • 如果此代码在没有任何错误的情况下运行,则已启用C ++ 17,请尝试此代码 链接
  • 确保当您运行代码时,终端看起来像这样
  • 图片
  • 注意:如果您不使用code Runner,我建议您使用该工具
  • 如果您正在使用 Windows 并且您的编译器不支持 c++17
  • 您可以从以下链接安装MinGw最新编译器(9.2.0)
  • 链接

1

1
我已经拥有了那个扩展,并且cppStandard设置为c++17。 - sabertooth

0

我没有在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"
    }
]
}

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接