前言
由于最近开发上使用了Bazel来构建,而且是用vscode进行远程后台开发,使用Remote Development插件,以前大多都是CLion开发C++,对vscode还是不太熟悉,这里简单记录下vscode的相关配置
vscode 编译流程
在工程目录下有个.vscode 文件夹,里面是所有 vscode 的配置文件,编译运行配置也在这里,涉及的配置文件有 launch.json和 tasks.json,不存在的话就自己创建即可
大致执行流程是这样的:
- vscode 会先遍历 launch.json,找到configurations,也就是入口启动配置
- 在configurations里会先找preLaunchTask,也就是启动前的任务
- 拿到configurations.preLaunchTask去 tasks.json 里找对应的具体任务
- tasks.json 里有个tasks列表,里面是多个 task任务,task名字是 label 字段,也就是和configurations.preLaunchTask对应
- 找到 task 后先执行configurations.preLaunchTask,然后执行configurations.program
具体配置
搞清楚大致流程后,就可以参照样例写自己的配置文件了
我这里是测试逻辑,编译后的执行文件在${workspaceFolder}/bazel-bin/server/TestServer,可以根据自己实际情况修改
launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"preLaunchTask": "BuildDebug",
"type": "cppdbg",
"MIMode": "gdb",
"request": "launch",
"program": "${workspaceFolder}/bazel-bin/server/TestServer",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
}
]
}
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "BuildDebug",
"type": "shell",
"command": "echo ${workspaceFolder} && bazel build //server:TestServer -c dbg",
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}