VS Code中G++可运行程序但调试失败:目标程序路径不存在
VS Code G++调试提示程序不存在的排查与解决
问题说明
在Visual Studio Code中使用G++编译运行程序一切正常,但启动调试时出现错误:
launch: program c:filepath\build\debug\outdebug does not exist
相关配置文件如下:
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "C/C++ Runner: Debug Session", "type": "cppdbg", "request": "launch", "args": [], "stopAtEntry": false, "externalConsole": true, "cwd": "c:/Users/name/CSC", "program": "c:/Users/name/CSC/build/Debug/outDebug", "MIMode": "gdb", "miDebuggerPath": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
tasks.json
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ], "version": "2.0.0" }
c_cpp_properties.json
{ "configurations": [ { "name": "windows-gcc-x86", "includePath": [ "${workspaceFolder}/**" ], "compilerPath": "C:/MinGW/bin/gcc.exe", "cStandard": "${default}", "cppStandard": "${default}", "intelliSenseMode": "windows-gcc-x86", "compilerArgs": [ "" ] } ], "version": 4 }
经检查,launch.json中指定的路径c:/Users/name/CSC/build/Debug/outDebug不存在对应文件及文件夹,重装G++后问题依旧。
核心原因
launch.json中配置的调试目标程序路径,与tasks.json里编译生成的可执行文件路径完全不匹配:
tasks.json会将编译后的exe文件输出到当前活动文件的目录下,命名为[文件名].exelaunch.json却试图去一个不存在的build/Debug目录找名为outDebug的程序,自然无法找到
解决步骤
1. 修改launch.json的程序路径与编译输出匹配
修改launch.json中的program和cwd字段,使用VS Code内置变量指向编译生成的exe文件:
{ "version": "0.2.0", "configurations": [ { "name": "C/C++ Runner: Debug Session", "type": "cppdbg", "request": "launch", "args": [], "stopAtEntry": false, "externalConsole": true, "cwd": "${fileDirname}", // 改为当前文件目录,与编译任务的cwd一致 "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", // 指向编译生成的exe "MIMode": "gdb", "miDebuggerPath": "gdb", "preLaunchTask": "C/C++: g++.exe build active file", // 新增:调试前自动执行编译任务 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
2. 关键配置说明
preLaunchTask:指定调试开始前要执行的编译任务(值为tasks.json中对应任务的label字段),确保每次调试前都会重新编译最新代码,避免因代码更新但exe未同步导致的找不到程序问题${fileDirname}:当前活动文件所在目录${fileBasenameNoExtension}:当前活动文件的文件名(不含后缀)
3. 验证修复
保存所有配置文件后,重新启动调试:
- 若配置正确,VS Code会先执行编译任务生成exe,再启动gdb调试该程序
- 若仍有问题,检查MinGW的gdb是否正确配置到系统环境变量中,确保
miDebuggerPath能找到gdb.exe
内容的提问来源于stack exchange,提问作者rob hiner
相关产品推荐
相关产品推荐

