VS Code:编译时更改命令行

时间:2020-08-03 12:54:31

标签: c++ visual-studio-code

程序:VS Code 1.47.3
打包使用:https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

#include <iostream>

int main()
{
    int a{7}; // it is not supported in VS code
    std::cout << a << '\n';
    return 0;
}

当我按下run时,终端显示如下命令-

$ cd“ / Users / xxxx / Desktop / C ++ / C ++ /” && g ++ example.cpp -o example &&“ / Users / xxxx / Desktop / C ++ / C ++ /” example

我想在编译时将基本命令更改为此

$ cd“ / Users / xxxx / Desktop / C ++ / C ++ /” && g ++ example.cpp -std = c ++ 17 -o example &&“ / Users / xxxx / Desktop / C ++ / C ++ /“示例

所以我不必每次执行时都键入'-std = c ++ 17'。

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

对不起,英语不好。

1 个答案:

答案 0 :(得分:0)

将配置更改为:

...
"args": [
    "-std=c++17",
    "-g",
    "${file}",
    "-o",
    "${fileDirname}/${fileBasenameNoExtension}"
],
...

编辑:自从您提到:

我尝试了,什么都没改变。正在执行的命令仍然相同。

可能您的VS Code的配置有问题,您应该考虑删除工作区中的.vscode文件夹并创建一个新的文件夹。我已经描述了正确的方法in this SO thread