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

WSL Ubuntu环境下VS Code调试C++代码失败求助

解决VS Code WSL Ubuntu环境C++调试报错问题

问题现象

  • 已在WSL Ubuntu中安装gdb、g++,终端可正常运行C++代码,但VS Code调试时触发报错
  • 终端输出报错:

&"warning: GDB: Failed to set controlling terminal: Operation not permitted\n"
[1] + Done "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-pyvj15od.ucf" 1>"/tmp/Microsoft-MIEngine-Out-sl4nyggt.r1b"

  • 调试控制台报错:

ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x8001189
Cannot insert breakpoint 2.
Cannot access memory at address 0x8001191

  • launch.json、settings.json未自动生成,当前tasks.json配置如下:
{
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: g++-11 build active file",
        "command": "/usr/bin/g++-11",
        "args": [
            "-fdiagnostics-color=always",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
],
"version": "2.0.0"
 }

解决步骤

1. 手动配置launch.json

VS Code未自动生成launch.json时,手动在.vscode目录下创建该文件,填入以下配置(确保和tasks.json的任务标签匹配):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++-11 - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++-11 build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

2. 修复GDB权限限制

执行以下命令临时关闭WSL的ptrace限制:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

若需永久生效,编辑/etc/sysctl.d/10-ptrace.conf文件,将kernel.yama.ptrace_scope = 1修改为kernel.yama.ptrace_scope = 0,之后重启WSL生效。

3. 重新生成带调试符号的可执行文件

确认tasks.json中已包含-g参数(你的配置已满足),该参数用于生成调试符号。执行VS Code的build任务,重新生成可执行文件,确保调试信息完整。

4. 检查环境兼容性

  • 确认WSL版本为2:执行wsl --list --verbose查看,若为WSL 1,通过wsl --set-version Ubuntu 2升级
  • 更新VS Code插件:将C/C++ Extension Pack和WSL插件更新至最新版本

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 07:42:54