在VS Code中配置task.json文件,以在Mac上使用特定版本的C ++编译C ++

时间:2020-03-28 19:35:17

标签: visual-studio-code compilation g++ clang

我正在尝试使用VS Code编译一些C ++代码,并且在使用其他C ++版本时遇到了一些麻烦。我想使用选项-std=c++17对其进行编译,因为有些事情我只需要测试C ++ 17中的工作(默认情况下,Clang使用C ++ 14)。 因此,我尝试编辑 tasks.json 文件以手动添加使用C ++ 17的选项。但是,即使这样做,也似乎无济于事。

最初,我仅编辑了 g ++构建活动文件options部分,但是由于它似乎无法正常工作,因此我将该选项添加到了所有任务中。不幸的是,这也无济于事。你能告诉我我在哪里犯错了吗? 您可以在下面的 tasks,json 文件中找到 tasks 部分。

"tasks": [
        {
            "type": "shell",
            "label": "clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++17"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "type": "shell",
            "label": "clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++17"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        },
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++17"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]

1 个答案:

答案 0 :(得分:0)

好吧,答案很简单,我实际上是在做正确的事情,但是在错误的地方。

首先,可以将-std=c++17手动添加到您的任何任务中,也可以仅创建一个具有特定名称的单独任务。例如(摘自VS Code网站)

        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}",
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }

这是一个名称为clang++ build active file且使用std=c++17作为C ++版本的任务。然后,不必按运行代码按钮,而必须使用 Terminal-> Run build task 选项。

我最初虽然有一种方法可以覆盖 Run code 按钮行为以使用不同的C ++版本,但是我想唯一的方法是通过添加新的/编辑旧的任务。