VSCode调试多个C++文件时出现prelaunch任务错误如何解决?
VSCode多C++文件调试prelaunch任务错误修复方案
核心错误原因
tasks.json中配置的编译工作目录错误:你当前将编译任务的options.cwd设置为C:\\MinGW\\bin,编译器执行时会在该目录下匹配*.cpp规则,无法找到你项目文件夹内的C++源文件,导致编译失败触发prelaunch任务报错。
次要待修复项
launch.json中的miDebuggerPath路径存在转义错误,C:\\MinGW\bin\\gdb.exe中\bin的反斜杠未完成转义,可能导致gdb调试器无法正常识别。
修复后的配置文件
tasks.json
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-g", "*.cpp", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" } }, { "type": "shell", "label": "g++ build & run active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "*.cpp", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" } } ] }
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "g++.exe build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "g++.exe build active file" }, { "name": "g++ build & run active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "g++ build & run active file" } ] }
注意事项
- 所有C++源文件需要和当前调试打开的文件在同一目录下,
*.cpp规则才能正常匹配所有源文件 - 配置修改完成后重启VSCode再执行调试,避免旧配置缓存导致异常
内容的提问来源于stack exchange,提问作者Ken Za
相关产品推荐
相关产品推荐

