You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10下WSL Ubuntu中VS Code无法运行/调试C代码

你遇到的核心问题是配置文件混用了Windows和WSL环境的路径——WSL的Shell无法识别Windows的C:\盘符路径,导致找不到MinGW的gcc,同时默认编译器设置也和WSL环境不匹配。以下是针对性的修复方案:

1. 修改tasks.json(编译任务配置)

删除适配Windows的任务,保留并修改WSL环境的编译任务,使用Ubuntu系统自带的gcc:

{
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: gcc build active file",
        "command": "/usr/bin/gcc",
        "args": [
            "-fdiagnostics-color=always",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task for WSL Ubuntu gcc"
    }
],
"version": "2.0.0"
}
  • 核心调整:command改为WSL原生gcc路径/usr/bin/gcc,输出路径用Linux风格正斜杠,去掉.exe后缀(WSL生成ELF格式可执行文件)。

2. 修改settings.json(默认编译器设置)

将默认编译器路径改为WSL的gcc:

{
"C_Cpp.default.compilerPath": "/usr/bin/gcc"
}

3. 调整launch.json(调试配置)

确保预编译任务标签与tasks.json一致,同时调试目标路径与编译输出匹配:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "miDebuggerPath": "/usr/bin/gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "C/C++: gcc build active file"
    }
]
}
  • 核心调整:preLaunchTask与tasks.json的label完全一致;program路径对应编译生成的可执行文件。

验证步骤

  1. 保存所有修改后的配置文件
  2. 关闭VS Code后重新打开WSL工作区
  3. 打开目标C代码文件,按F5尝试调试

内容的提问来源于stack exchange,提问作者Sterling

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 21:07:20