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

如何阻止VS Code调试C++时异常信息输出到终端?

更换gdb路径后,VS Code调试C++异常信息显示在终端的解决方法

更换gdb路径后,调试C++程序时,异常信息不再在调试控制台展示,而是输出到终端中。以下是当前使用的launch.json和tasks.json配置:

当前配置文件

launch.json

{
    // 使用 IntelliSense 了解相关属性。
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动 ",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "preLaunchTask": "C/C++: g++.exe 生成活动文件",
            "MIMode": "gdb",
            "miDebuggerPath": "D:/MinGW/mingw64_13/x86_64-13.2.0-release-win32-seh-ucrt-rt_v11-rev0/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                },
                {
                    "description": "在调试中跳过属于std命名空间的函数(已编译过)",
                    "text": "skip -rfu std::.*",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\MinGW\\mingw64_13\\x86_64-13.2.0-release-win32-seh-ucrt-rt_v11-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

问题分析与解决方法

  • 检查gdb版本兼容性
    你使用的是gcc 13.2.0配套的gdb,部分高版本gdb与VS Code的C/C++插件可能存在输出重定向的兼容性问题。可以尝试:

    • 确认当前VS Code的C/C++插件版本是否为最新,更新插件后重试。
    • 若问题仍存在,可暂时换回之前能正常工作的gdb版本,验证是否为版本适配问题。
  • 添加输出重定向配置
    在launch.json的调试配置项中添加以下两个属性,强制将程序输出和调试信息重定向到调试控制台:

    "redirectOutput": true,
    "internalConsoleOptions": "openOnSessionStart"
    

    修改后的完整调试配置片段如下:

    {
        "name": "(gdb) 启动 ",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "preLaunchTask": "C/C++: g++.exe 生成活动文件",
        "MIMode": "gdb",
        "miDebuggerPath": "D:/MinGW/mingw64_13/x86_64-13.2.0-release-win32-seh-ucrt-rt_v11-rev0/mingw64/bin/gdb.exe",
        "redirectOutput": true,
        "internalConsoleOptions": "openOnSessionStart",
        "setupCommands": [
            {
                "description": "为 gdb 启用整齐打印",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            },
            {
                "description": "将反汇编风格设置为 Intel",
                "text": "-gdb-set disassembly-flavor intel",
                "ignoreFailures": true
            },
            {
                "description": "在调试中跳过属于std命名空间的函数(已编译过)",
                "text": "skip -rfu std::.*",
                "ignoreFailures": true
            }
        ]
    }
    
  • 验证编译配置
    确认tasks.json中的编译参数-g已正确添加,确保生成的可执行文件包含完整调试信息,这是调试信息正常展示的基础。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 02:37:12