虽然没有明确说明,但听起来你在 Windows 上并使用 Cygwin make。
基本上使用约翰的建议,这是一个完整tasks.json
的用于cygpath -m
将斜杠传递给make
:
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "d:/cygwin64/bin/sh",
"args": [
"-c",
"make APPDIR=$(cygpath -m '${workspaceFolder}')"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
这是一个Makefile
要调用的示例:
$(info APPDIR is "$(APPDIR)")
helloworld.exe: helloworld.cpp
g++ -g -Wall -std=c++11 -o $@ helloworld.cpp
当我按 Ctrl+Shift+B 调用此任务时,我在终端窗口中看到:
> Executing task in folder cpphello: d:/cygwin64/bin/sh -c "make APPDIR=$(cygpath -m 'D:\wrk\learn\vscode\cpphello')" <
APPDIR is "D:/wrk/learn/vscode/cpphello"
make: 'helloworld.exe' is up to date.
Terminal will be reused by tasks, press any key to close it.
这使用-m
(混合)开关来cygpath
获得看起来像 Windows 路径但使用正斜杠的内容。 cygpath
有其他选择;见cygpath --help
。
这里有两个微妙之处:
我明确指定了路径sh
。那是因为我的 $PATH 上还有适用于 Windows 的 git,它位于我的 Cygwin 路径之前,因此 vscode 将使用它
git
。但是 git for Windows 也有sh.exe
,如果在这里使用那个,会make
因 Cygwin DLL 错误而爆炸。
我不得不将默认的 VSCode shell 更改为cmd.exe
,而默认值为powershell.exe
. powershell 的问题是 VSCode 在向其传递参数时使用单引号,而我的解决方案要求 VSCode 使用双引号,它使用cmd.exe
. 要更改外壳,请使用面板中的“终端:选择默认外壳”命令 (Ctrl+Shift+P)。
bash
最后,我要指出,如果在您的情况下,您可以创建一个中间shell 脚本而不是直接调用,那么所有这些废话都可以避免make
。该tasks.json
语言不是很强大,而古怪的 shell VSCode 知道如何在 Windows 上调用(即cmd.exe
and powershell.exe
)增加了额外的复杂性和脆弱性。