最近对 的更改tasks.json
似乎为列出的每个任务提供了一个命令。请参阅https://code.visualstudio.com/docs/editor/tasks,这让很多事情变得毫无意义。
这个答案最初是针对更复杂的解决方案,但在接受的答案中提供的简单 shell 运行器任务格式证明更有用。请参阅下面的内容以了解现在的情况。
这里的限制是 VS Code 仅限于给定工作区的单个高级构建任务/命令。允许多个子任务,但仅限于使用顶级“命令”,但可以提供不同的“参数”。这将非常适合使用类似于 make、ant 或 msbuild 的构建系统的环境。例如;
{
"version": "0.1.0",
"command": "make", // command must appear here
"tasks" : [
{
"taskName": "clean",
"suppressTaskName": false, // false by default
//"command": "somethingelse", // not valid here
"args": ["${file}"] // if required
},
{
"taskName": "install"
// ...
}
]
}
有两种选择;
有一个自定义脚本尝试仅给定 task.json 中的参数来运行编译/执行。
-- the shell file would be a simple
"$@" # run what I get
-- the tasks.json
"args": ["clang++", "-std=c++14", "-O2", "${file}"]
让可执行文件运行 ( ./a.out
) 更加努力。简单地将它作为参数添加是行不通的,如果它在那里,则需要 shell 脚本来执行它。
在给定文件扩展名和文件名的情况下,将输出切换和执行到自定义脚本。事实证明,这更容易实现,并且在 shell 脚本中提供了更多控制。
{
"version": "0.1.0",
"isShellCommand": true,
"taskName": "generic build",
"showOutput": "always",
"args": ["${fileExtname}", "${file}"]
"command": "./.vscode/compileme.sh", // expected in the "local settings" folder
//"command": "~/compileme.sh", // if in HOME folder
}
还有shell脚本compileme.sh;
#!/bin/sh
# basic error checking not shown...
echo "compilation being executed with the arguments;"
echo "$@"
filetype=$1
file=$2
if [ $filetype = ".cpp" -o $filetype = ".cxx" ] ; then
clang++ -std=c++14 -Wall -Wextra -pedantic -pthread $file && ./a.out
elif [ $filetype = ".c" ]
then
clang -std=c11 -Wall -Wextra -pedantic -pthread $file && ./a.out
elif [ $filetype = ".sh" ]
then
$file
elif [ $filetype = ".py" ]
then
python $file
else
echo "file type not supported..."
exit 1
fi
鉴于上面列出的选项,第二个选项更可取。此实现适用于 OS X,但可以根据需要轻松移植到 Linux 和 Windows。我将密切关注这一点,并尝试跟踪对 VS Code 构建任务、基于文件的构建或对多个命令的支持的更改,这可能是一个受欢迎的补充。
我的 tasks.json 支持一些跑步者,并且默认为构建打印消息作为提醒。它使用外壳作为跑步者,现在看起来像......
{
"version": "0.1.0",
"isShellCommand": true,
"taskName": "GenericBuild",
"showOutput": "always",
"command": "sh",
"suppressTaskName": false,
"args": ["-c"],
"tasks": [
{
"taskName": "no build",
"suppressTaskName": true,
"isBuildCommand": true,
"args": [
"echo There is no default build task, run a task by name..."
]
},
{
"taskName": "cpp",
"suppressTaskName": true,
"args": [
"clang++ -std=c++14 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"
]
},
{
"taskName": "shell",
"suppressTaskName": true,
"args": [
"\"${file}\""
]
},
{
"taskName": "python",
"suppressTaskName": true,
"args": [
"python \"${file}\""
]
},
{
"taskName": "c",
"suppressTaskName": true,
"args": [
"clang -std=c11 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"
]
}
]
}