【问题标题】:Configurate tasks.json file in VS Code to compile C++ with specific version of C++ on Mac在 VS Code 中配置 tasks.json 文件以在 Mac 上使用特定版本的 C++ 编译 C++
【发布时间】:2020-03-28 19:35:17
【问题描述】:

我正在尝试使用 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"
        }
    ]

【问题讨论】:

  • 似乎没有任何效果”和“也没有用”到底是什么意思?编译失败了吗?您在 VS Code 的控制台输出中收到了哪些错误或日志消息?你是如何执行任务的? (任务:运行任务> g++ 构建活动文件)?您是否尝试过在 VS Code 之外运行 g++ 命令以确认它可以在您的机器上正确编译?
  • VS Code 中的标准编译(-std=c++11 及更早版本)以及终端中的编译工作正常。不过我找到了解决问题的方法,所以我想我会自己发布答案

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


【解决方案1】:

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

首先,您可以手动将-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++ 版本,但我想唯一的方法是通过添加新的/编辑旧的任务。

【讨论】:

    猜你喜欢
    • 2022-11-28
    • 2019-10-23
    • 2021-08-02
    • 2022-01-09
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    相关资源
    最近更新 更多