WSL2中VS Code无法识别C编译器问题咨询
解决WSL2 Ubuntu下VS Code C/C++开发无编译器配置选项的问题
先检查WSL环境中是否安装C/C++编译器,执行命令:
gcc --version或g++ --version
若未安装,通过sudo apt update && sudo apt install gcc g++ gdb安装完整编译调试工具链。确认VS Code的C/C++扩展已在WSL环境激活:
点击VS Code左下角远程连接图标,确保当前连接环境为WSL: Ubuntu,扩展列表中C/C++扩展显示"已安装在WSL: Ubuntu"。手动生成编译与调试配置文件:
- 打开
helloworld.cpp,按Ctrl+Shift+B,选择"创建tasks.json文件",选"Others"模板后替换为以下编译任务配置:
{ "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ build active file", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": true }, "detail": "编译器: /usr/bin/g++" } ] }- 按
F5触发调试,选择"GDB/LLDB"后选"g++ build and debug active file"自动生成launch.json,或手动创建如下内容:
{ "version": "0.2.0", "configurations": [ { "name": "g++ - Build and debug active file", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++ build active file", "miDebuggerPath": "/usr/bin/gdb" } ] }- 打开
重启VS Code的WSL连接:
点击左下角远程连接图标,选择"关闭远程连接",重新打开项目文件夹并连接到WSL环境后再尝试运行调试。
内容的提问来源于stack exchange,提问作者MG-LSJ
相关产品推荐
相关产品推荐

