多文件调试配置
在上一篇文章1.1 VSCode中使用Code runner配置C++单文件多文件编译,并自动创建bin文件夹把.exe文件放入其中中,已经知道如何通过code runner编译多文件。
那么如何调试,只需要在tasks.json和launch.json两个设置文件中做一些微小的调整即可。
前言
本文是为了学习方便,工程上还是分单文件编译再进行链接为好。
编译文件tasks.json的调整
注意:我们都是在main函数所在.cpp文件下进行的编译,生成的可执行文件.exe也都以main函数所在.cpp文件名命名。
只需要在这里需改一点就可以。
"args": [
"-g",
"${fileDirname}/*.cpp", // 只需在此修改为/*.cpp即可
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe" // 可执行文件以当前cpp文件命名
],
附上自己的tasks.json和launch.json设置文件
tasks.json文件:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "D:\\Programming\\MinGW-w64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${fileDirname}/*.cpp",
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
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++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Programming\\MinGW-w64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}