如何使用任务在Visual Studio代码中运行Cmake

时间:2018-03-31 05:19:18

标签: c++ cmake visual-studio-code vscode-tasks

我正在尝试使用ctrl+shift+B运行cmake,如下所示:

{
"version": "2.0.0",
"tasks": [
    {
        "label": "cmake",
        "type": "shell",
        "options": {
            "cwd": "${workspaceRoot}/build"
        },
        "command": "cmake ${workspaceRoot} -G \"MinGW Makefiles\"",
        (...)
    },
    {
        "label": "make",
        "type": "shell",
        "command": "mingw32-make.exe",
        "options": {
            "cwd": "${workspaceRoot}/build"
        },
       (...),
        "dependsOn":["cmake"]
    },
    {
        "label": "build",
        "type": "shell",
        "options": {
            "cwd": "${workspaceRoot}/build"
        },
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "dependsOn": ["make"]
    }
]
}

但无论我做什么它都运行在$ {workspaceRoot}而不是$ {workspaceRoot} / build one:

Executing task in folder cpppractice: cmake C:\workspace\cpp\cpppractice -G "MinGW Makefiles"

这种方法有什么问题吗?据我所知,cwd项中的options变量应该有效。

2 个答案:

答案 0 :(得分:1)

您错误地将参数传递给cmake。整个字符串cmake ${workspaceRoot} -G "MinGW Makefiles"被视为命令名称。参数必须列在args数组中。

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cmake",
            "type": "shell",
            "options": {
                "cwd": "${workspaceRoot}/build"
            },
            "command": "cmake",
            "args": [
                "${workspaceRoot}",
                "-G",
                "\"MinGW Makefiles\""
            ]
        },
        ...
    ]
}

答案 1 :(得分:1)

{
    "label": "cmake",
    "type": "shell",
    "options": {
        "cwd": "${workspaceRoot}/build"
    },
    "command": "cmake \"MinGW Makefiles\"  ${workspaceRoot}",
},

这很有效,似乎"执行任务在文件夹cpppractice:"是不准确的,它是在正确的位置执行它,为什么它以前没有工作我认为它错误地解析args?我无法确认,但这是输出:

Executing task in folder cpppractice: cmake "MinGW Makefiles"  
C:\workspace\cpp\cpppractice <

-- Configuring done
-- Generating done
-- Build files have been written to: C:/workspace/cpp/cpppractice/build

之前的意思是不能使用发电机&#34; MinGW&#34;这意味着它将参数与#34; MinGW Makefiles&#34;分开。 经过一些修修补补后,我发现这也是一个答案:

{
        "label": "cmake",
        "type": "shell",
        "options": {
            "cwd": "${workspaceRoot}/build"
        },
        "command": "cmake",
        "args": [
            "-G",
            "'MinGW Makefiles'",
            "./.."
        ],
        ...
    },

我实际上发现第二种方法有点干净但两种方法都相同,所以为了将参数作为字符串传递,你必须使用单引号,如下所示:

...
"command":"echo",
"args": [
        "'Za Warudo!'",
    ],
...