本文转载于vscode中preLaunchTask“g++”已终止,退出代码为1的解决方案
问题描述
在VSCode中配置C++调试环境时,出现了如下错误提示:
preLaunchTask“g++”已终止,退出代码为1。
解决方法
复制以下配置文件 launch.json 和 tasks.json
launch.json
注意:需要修改 "miDebuggerPath"后面内容,修改成自己的…/gdb.exe 路径。
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"preLaunchTask": "build",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/Program Files (x86)/CodeBlocks/MinGW/bin/gdb.exe", // 这里要修改为自己所安装的gdb.exe路径。最后的/gdb.exe无需更改。
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"windows": {
"command": "g++",
"args": [
"-ggdb",
"\"${file}\"",
"--std=c++11",
"-o",
"\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
]
}
}
]
}
总结
按上述修改完成后,可以成功调试C++程序。