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

rjee0c15  于 2023-10-20  发布在  其他
关注(0)|答案(2)|浏览(143)

我试着用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上:

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

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

p3rjfoxz

p3rjfoxz1#

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

这工作,它似乎是“在文件夹cpppractice执行任务:“是不准确的,它是在正确的地方执行它,至于为什么它以前没有工作,我认为它的解析参数不正确?我无法确认,但这是输出:

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

以前它是compalining关于不能使用生成器“MinGW”这意味着它分离的参数“MinGW Makefiles”。经过一番修改,我发现这也是一个答案:

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

实际上我发现第二种方法更简洁一点,但两者的工作原理是一样的,所以为了将参数作为字符串传递,你必须使用单引号,如下所示:

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

mnemlml82#

错误地将参数传递给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\""
            ]
        },
        ...
    ]
}

相关问题