如何在VS2022中调试WSL Ubuntu下用make编译的C++项目?
VS2022文件夹式项目配合WSL make编译调试的问题修复
问题背景
必须在Ubuntu WSL环境下用make编译C++项目,同时使用VS2022作为IDE,且只能采用无.sln/.vcxproj的文件夹式项目。配置tasks.vs.json和launch.vs.json后,调试时出现错误。
错误信息
Starting: "wsl" "/usr/bin/gdb --interpreter=mi"
/bin/bash: line 1: /usr/bin/gdb --interpreter=mi: No such file or directory
"wsl" exited with code 127 (0x7F).
原配置文件
launch.vs.json
{ "version": "0.2.1", "defaults": {}, "configurations": [ { "type": "cppdbg", "name": "WSL Launch", "request": "launch", "project": "main.exe", "program": "/mnt/c/Task1/main.exe", "args": [], "stopAtEntry": false, "cwd": "/mnt/c/Task1", "environment": [], "externalConsole": false, "pipeTransport": { "pipeCwd": "", "pipeProgram": "wsl", "pipeArgs": [], "debuggerPath": "/usr/bin/gdb" }, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build" } ] }
tasks.vs.json
{ "version": "0.2.1", "tasks": [ { "taskLabel": "Task-2024-14-10", "appliesTo": "/", "type": "launch" }, { "taskName": "build", "appliesTo": ".*", "type": "command", "command": "wsl", "args": [ "make" ], "label": "Build with WSL" }, { "taskName": "run", "appliesTo": ".*", "type": "command", "command": "wsl", "args": [ "./main.exe" ], "label": "Run Application in WSL" }, { "taskLabel": "Task-2024-14-10", "appliesTo": "/", "type": "launch" }, { "taskLabel": "Task-main", "appliesTo": "main.cpp", "type": "launch" }, { "taskLabel": "Task-\u00DCbung 1", "appliesTo": "/", "type": "launch" } ] }
问题分析
- gdb调用参数格式错误:launch配置中
pipeTransport.pipeArgs为空,导致VS将/usr/bin/gdb --interpreter=mi作为一个完整的命令字符串传给WSL。WSL会尝试查找名为/usr/bin/gdb --interpreter=mi的可执行文件,自然找不到,触发127错误。 - 可执行文件路径错误:Linux环境下编译的程序默认无
.exe后缀,原配置中program和run任务的args使用.exe后缀,会导致找不到编译产物。 - 无效配置冗余:tasks.vs.json中存在多个重复无用的
launch类型任务,会干扰配置解析。
修复方案
修正launch.vs.json
{ "version": "0.2.1", "defaults": {}, "configurations": [ { "type": "cppdbg", "name": "WSL Launch", "request": "launch", "program": "/mnt/c/Task1/main", // 去掉.exe后缀,匹配Linux编译产物 "args": [], "stopAtEntry": false, "cwd": "/mnt/c/Task1", "environment": [], "externalConsole": false, "pipeTransport": { "pipeCwd": "", "pipeProgram": "wsl", "pipeArgs": ["--", "/usr/bin/gdb", "--interpreter=mi"], // 拆分gdb命令与参数 "debuggerPath": "/usr/bin/gdb" }, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build" } ] }
- 移除无效的
"project": "main.exe"字段 - 修改
program路径为Linux下的可执行文件(假设make编译出的是main) - 给
pipeArgs添加["--", "/usr/bin/gdb", "--interpreter=mi"],让WSL正确拆分执行gdb命令
修正tasks.vs.json
{ "version": "0.2.1", "tasks": [ { "taskName": "build", "appliesTo": ".*", "type": "command", "command": "wsl", "args": [ "make" ], "label": "Build with WSL" }, { "taskName": "run", "appliesTo": ".*", "type": "command", "command": "wsl", "args": [ "./main" // 去掉.exe后缀 ], "label": "Run Application in WSL" } ] }
- 删除所有无用的重复
launch类型任务 - 修改run任务的
args为./main,匹配Linux编译产物
可行性说明
这种无.sln/.vcxproj的文件夹式项目完全可行。VS2022支持通过自定义tasks和launch配置,直接对接WSL环境的make编译和gdb调试,无需创建正式VS项目,只要配置符合WSL和cppdbg的要求即可正常工作。
内容的提问来源于stack exchange,提问作者Bugsia
相关产品推荐
相关产品推荐

