如何 运行 在 Visual Studio 中使用任务编写代码
How to run Cmake in Visual Studio Code using tasks
我正尝试 运行 cmake 与 ctrl+shift+B
像这样:
{
"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"]
}
]
}
但无论我做什么 运行s on ${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\""
]
},
...
]
}
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake \"MinGW Makefiles\" ${workspaceRoot}",
},
这个可以,好像是"Executing task in folder 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
之前抱怨无法使用生成器 "MinGW",这意味着它将参数 "MinGW Makefiles" 分开。
经过一些修补,我发现这也是一个答案:
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake",
"args": [
"-G",
"'MinGW Makefiles'",
"./.."
],
...
},
实际上我发现第二种方法更简洁一些,但两者的工作原理相同,因此为了将参数作为字符串传递,您必须像这样使用单引号:
...
"command":"echo",
"args": [
"'Za Warudo!'",
],
...
我正尝试 运行 cmake 与 ctrl+shift+B
像这样:
{
"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"]
}
]
}
但无论我做什么 运行s on ${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\""
]
},
...
]
}
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake \"MinGW Makefiles\" ${workspaceRoot}",
},
这个可以,好像是"Executing task in folder 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
之前抱怨无法使用生成器 "MinGW",这意味着它将参数 "MinGW Makefiles" 分开。 经过一些修补,我发现这也是一个答案:
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake",
"args": [
"-G",
"'MinGW Makefiles'",
"./.."
],
...
},
实际上我发现第二种方法更简洁一些,但两者的工作原理相同,因此为了将参数作为字符串传递,您必须像这样使用单引号:
...
"command":"echo",
"args": [
"'Za Warudo!'",
],
...