如何在VS Code tasks.json中创建合并make clean与make all的Rebuild任务
在VS Code中创建顺序执行make clean和make all的Rebuild任务
嘿,我来帮你搞定这个Rebuild任务的创建!根据你现有的tasks.json配置,有两种常用的方式来实现按顺序执行make clean和make all的Rebuild任务,我都给你列出来:
方法一:通过任务依赖实现(推荐,更灵活)
这种方式需要先确保你已经有单独的make clean和make all任务(就像你现有的make debug那样),然后新建一个Rebuild任务,让它按顺序依赖这两个任务。修改后的tasks.json如下:
{ "version": "2.0.0", "tasks": [ { "label": "make debug", "type": "shell", "command": "make", "options": { "cwd": "${workspaceRoot}/Debug" }, "problemMatcher": ["$gcc"] // 可选,用来捕获编译错误,方便定位问题 }, { "label": "make clean", "type": "shell", "command": "make clean", "options": { "cwd": "${workspaceRoot}/Debug" }, "problemMatcher": [] }, { "label": "make all", "type": "shell", "command": "make all", // 如果你的Makefile里all是默认目标,这里也可以直接写"make" "options": { "cwd": "${workspaceRoot}/Debug" }, "problemMatcher": ["$gcc"] }, { "label": "Rebuild", "type": "shell", "dependsOn": ["make clean", "make all"], "dependsOrder": "sequence", // 关键!强制先执行clean,完成后再执行all "problemMatcher": [] } ] }
关键点说明:
dependsOrder: "sequence"必须加上,不然VS Code可能会并行执行两个任务,那就达不到先清理再编译的效果了。- 这种方式的好处是,你还可以单独执行
make clean或make all任务,灵活性更高。
方法二:直接串联命令(简单快捷)
如果你不想单独定义make clean和make all,也可以直接在Rebuild任务的命令里把两个操作串起来,用&&确保前一个命令执行成功后再执行下一个。修改后的配置如下:
{ "version": "2.0.0", "tasks": [ { "label": "make debug", "type": "shell", "command": "make", "options": { "cwd": "${workspaceRoot}/Debug" }, "problemMatcher": ["$gcc"] }, { "label": "Rebuild", "type": "shell", "command": "make clean && make all", // 用&&串联两个命令 "options": { "cwd": "${workspaceRoot}/Debug" }, "problemMatcher": ["$gcc"] } ] }
小提示:
修改完tasks.json后,按下Ctrl+Shift+B(Windows/Linux)或Cmd+Shift+B(Mac),就能在弹出的任务列表里看到新的Rebuild任务,选择它就可以一键执行清理再编译啦!
内容的提问来源于stack exchange,提问作者Tim Ashton
相关产品推荐
相关产品推荐

