编辑launch.json。
1、将 program 参数修改如下:
“program”: “C:\\Windows\\system32\\cmd.exe”
意思就是运行 cmd 窗口
2、在 args 参数中增加/K,具体配置如下:
“args”: [
“/C”, “${fileDirname}/${fileBasenameNoExtension}.exe”,”&”,”pause”
], // 程序调试时传递给程序的命令行参数,一般设为空即可
这样配置好后,调试C++代码,运行结束后,会自动停止在对应的return 0所在,而不需要辛苦在每个return 0设置断点。
完整的配置如下:
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "C:\\Windows\\system32\\cmd.exe",
"args": ["/C","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "E:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}