’
点击debug后如上图所示,提示could not find the task ‘g++ build active file’.(现在知道了是因为prelaunchtask没有找到label为这个的task)
尝试了一下该文所说方法
修改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": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/test", //test是自己取的名字
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
结果:
没有变化。提示是要configure task,应该是缺少tasks.json文件。
配置task:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${workspaceFolder}/ch02/test.cpp",
"-o",
"test"
]
}
]
}
再debug,结果:
还是不对。猜测是tasks.json和launch.json的preLaunchTask不一致所致。
全部改为test再试:
提示launch的program 路径 does not exist。(估计此时的task已经运行了一次了,但到program处停下来了)
我再试试先执行task,ctrl+shift+b;
成功生成test文件
但ch02中有个test,外面还有一个test,这个原因应该是命令中的问题。
可见需要先配置 tasks文件,build task后才能debug。
补充:
但ch02中有个test,外面还有一个test,这个原因应该是命令中的问题。
问题是task配置的参数中只写了"test",改为下列参数后生成的test就只在ch02文件夹中了。
"args": [
"-g",
"${workspaceFolder}/ch02/test.cpp",
"-o",
"./ch02/test"
],
再debug检验一下之前debug使用的test是不是ch02中的那个test。
debug成功,看来确实是使用了ch02中的test。但是为什么之前会在ch02外和ch02内同时生成test呢,既然没有指定路径,一般来说应当是在当前路径生成,那么就只应当在外面有一个test。
注意到.vscode文件夹是在C++算法文件夹下的,
猜测:莫非是vscode的某种自动检测造成了文件夹内也生成test?
我们来看看launch的配置中的与路径相关的地方:
"program": "${workspaceFolder}/ch02/test",
"preLaunchTask": "test",
将preLaunchTask改为:
"preLaunchTask": "./ch02/test",
再debug,
注意到此时task的配置
"label": "test",
猜测是preLaunchTask与task的label不一致以至于debug时不能找到task。
官方文档说:
preLaunchTask——
to launch a task before the start of a debug session, set this attribute to the name of a task specified in tasks.json (in the workspace’s .vscode folder). Or, this can be set to ${defaultBuildTask} to use your default build task.
考虑到tasks.json和launch.json同在.vscode文件夹中,则launch.json通过preLaunchTask来启动task时只需要task的label即可。
可知label和preLaunchTask是必须相等的,而preLaunchTask是通过匹配label来寻找tasks.json中的某个task。
注意tasks.json文件中是可以配置多个task的,所以自然需要不同的label来区别不同task,这些task是在tasks.json文件中的,与build task后生成的test没有关系,而test是由launch.json中的program关联的。
再次将task改为:
"args": [
"-g",
"${workspaceFolder}/ch02/test.cpp",
"-o",
"test"
],
build task结果发现只在ch02外生成了test,。。。。看来是第一次记错了?
此时再debug,
可见launch的program才是与build tasks后生成的test相关联的,此时由于program找不到ch02内的test,于是发生了错误。如果此时我将外面的test剪切到ch02内,
再debug,
发现居然在成功的同时,ch02外又生成了一个test,看来之前的并非我眼花了,而是在debug时program链接到了ch02中的test运行,而prelaunchtask又再一次运行了task而按照原来的task配置在ch02外面生成了一个test。
为了检验我的猜测,删除ch02外的test,修改task,
"tasks": [
{
"label": "test",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${workspaceFolder}/ch02/test.cpp",
"-o",
"./ch01/test" //该task build会在ch01中生成test
],
此时launch中
"program": "${workspaceFolder}/ch02/test", //与ch02内的test相连
"preLaunchTask": "test", //与tasks中的label为test的task相连
debug成功且果然符合预期,猜测正确。那么既然build task的功能就是生成test,而我们已经先build task生成了test,然后再运行launch通过program与test相连来debug,launch中的prelaunchTask显得多余,我估计此时注释掉它是对debug没有影响的,试验一下:
试验成功!
那么prelaunchtask为什么会多余呢?我猜测debug其实并不需要先build task来生成test,而可以直接通过launch.json完成。(前提是tasks.json和launch.json均配置完毕)过程为先通过prelaunchtask启动task生成test,然后program链接test进行debug。试验一下:
先将test全删掉,配置task和launch,
task
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${workspaceFolder}/ch02/test.cpp",
"-o",
"./ch02/test" //注意此处的.指的是workspace即C++算法文件夹
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
launch
{
// 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": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/ch02/test",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "test",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
开始debug,
试验成功!至此vscode的task与launch配置研究圆满结束!