Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用任务在Visual Studio代码中运行Cmake_C++_Cmake_Visual Studio Code_Vscode Tasks - Fatal编程技术网

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

C++ 如何使用任务在Visual Studio代码中运行Cmake,c++,cmake,visual-studio-code,vscode-tasks,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

我尝试使用
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"

这种方法有什么错误吗?据我所知,
options
项中的
cwd
变量应该可以工作。

您错误地将参数传递给
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\""
            ]
        },
        ...
    ]
}
这是可行的,似乎“Executing task in folder cppractice:”是不准确的,它在正确的位置执行它,至于为什么它以前不工作,我认为它解析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
实际上,我发现第二种方法更简洁,但两者的工作原理相同,因此,为了将参数作为字符串传递,必须使用单引号,如下所示:

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

我已经试过了,但是输出是一样的:
在文件夹cppractice:cmake C:\workspace\cpp\cppractice-G“MinGW Makefiles”中执行任务
{
        "label": "cmake",
        "type": "shell",
        "options": {
            "cwd": "${workspaceRoot}/build"
        },
        "command": "cmake",
        "args": [
            "-G",
            "'MinGW Makefiles'",
            "./.."
        ],
        ...
    },
...
"command":"echo",
"args": [
        "'Za Warudo!'",
    ],
...