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

VS Code使用gdb调试C++代码无报错但调试会话无限卡住问题求助

VS Code 搭配MinGW GDB调试C++代码无报错无限卡住问题排查

问题现象

编译过程始终正常,但调试启动后无任何报错,调试会话无限卡住无法推进。

运行环境配置

  • Win10 19043.1288 (21H1),系统默认语言为俄语
  • VS Code版本1.60.1
  • C/C++扩展版本1.7.1
  • MinGW从msys2-x86_64-20210725安装
  • gcc 11.2.0、gdb 11.1均已添加至系统PATH环境变量

测试代码(helloworld.cpp)

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

tasks.json配置

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe сборка активного файла",
            "command": "C:\\msys64\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-static-libstdc++"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "компилятор: C:\\msys64\\mingw64\\bin\\g++.exe"
        }
    ]
}

注:配置包含"-static-libstdc++"参数,不添加时生成的exe无法正常运行

launch.json配置

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - Сборка и отладка активного файла",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
            "logging": { "engineLogging": true },
            "setupCommands": [
                {
                    "description": "Включить автоматическое форматирование для gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe сборка активного файла"
        }
    ]
}

终端输出内容

(base) PS C:\Users\NAME\Documents\Ящик\Разработка\CPP_training\helloworld>  & 'c:\Users\NAME\.vscode\extensions\ms-vscode.cpptools-1.7.1\debugAdapters\bin\WindowsDebugLauncher.exe'
'--stdin=Microsoft-MIEngine-In-xrq1w4ni.vpd' '--stdout=Microsoft-MIEngine-Out-hz0moxmf.ebp' '--stderr=Microsoft-MIEngine-Error-ow1iwyo5.0ow'
'--pid=Microsoft-MIEngine-Pid-iwvvrfdm.b4e' '--dbgExe=C:\msys64\mingw64\bin\gdb.exe' '--interpreter=mi' 

调试控制台日志

1: (342) Wait for connection completion.
1: (869) ->=thread-group-added,id="i1"
1: (871) ->~"GNU gdb (GDB) 11.1\n"
1: (872) ->~"Copyright (C) 2021 Free Software Foundation, Inc.\n"
1: (872) ->~"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law."
1: (873) ->~"\nType \"show copying\" and \"show warranty\" for details.\n"
1: (875) ->~"This GDB was configured as \"x86_64-w64-mingw32\".\n"
1: (877) ->~"Type \"show configuration\" for configuration details.\n"
1: (877) ->~"For bug reporting instructions, please see:\n"
1: (878) ->~"<https://www.gnu.org/software/gdb/bugs/>.\n"
1: (878) ->~"Find the GDB manual and other documentation resources online at:\n    <http://www.gnu.org/software/gdb/documentation/>."
1: (879) ->~"\n\n"
1: (879) ->~"For help, type \"help\".\n"
1: (879) ->~"Type \"apropos word\" to search for commands related to \"word\".\n"
1: (885) ->(gdb)
1: (889) <-1001-gdb-set target-async on
1: (901) ->~"Warning: 'set target-async', an alias for the command 'set mi-async', is deprecated.\n"
1: (901) ->~"Use 'set mi-async'.\n\n"
1: (901) ->1001^done
1: (902) ->(gdb)
1: (902) ->&"\n"
1: (902) ->^done
1: (903) ->(gdb)
1: (906) 1001: elapsed time 18
1: (932) <-1002-enable-pretty-printing
1: (947) ->1002^done
1: (948) 1002: elapsed time 15
1: (948) ->(gdb)
1: (948) <-1003-interpreter-exec console "set pagination off"
1: (948) ->&"\n"
1: (948) ->^done
1: (949) ->(gdb)
1: (962) ->=cmd-param-changed,param="pagination",value="off"
1: (963) ->1003^done
1: (963) 1003: elapsed time 15
1: (963) ->(gdb)
1: (963) <-1004-gdb-set auto-solib-add on
1: (963) ->&"\n"
1: (964) ->^done
1: (964) ->(gdb)
1: (978) ->1004^done
1: (979) 1004: elapsed time 15
1: (979) <-1005-gdb-set solib-search-path c:\\Users\\NAME\\Documents\\Ящик\\Разработка\\CPP_training\\helloworld;
1: (979) ->(gdb)
1: (980) ->&"\n"
1: (980) ->^done
1: (980) ->(gdb)
1: (982) ->1005^done
1: (982) 1005: elapsed time 2
1: (982) ->(gdb)
1: (982) <-1006-environment-cd c:\\Users\\NAME\\Documents\\Ящик\\Разработка\\CPP_training\\helloworld
1: (982) ->&"\n"
1: (982) ->^done
1: (983) ->(gdb)

问题原因与解决方案

核心原因

触发的仍然是路径编码兼容问题,你的代码存放路径包含西里尔字符,GDB与VS Code C++扩展的交互过程中对非ASCII路径的处理存在兼容缺陷,即使新版本扩展修复了部分Unicode问题,在俄语系统环境+西里尔字符路径的场景下仍会触发卡住问题。

解决方案

按优先级依次尝试:

  1. 移动项目到纯ASCII路径
    将项目整体移动到不含任何非英文字符的路径下,路径中不要出现俄语、中文等非ASCII字符,重试调试即可正常运行。
  2. 修改系统区域编码为UTF-8
    如果不想移动项目,可开启Win10的UTF-8全局编码支持:
  • 打开控制面板→区域→管理选项卡→更改系统区域设置
  • 勾选Beta版:使用Unicode UTF-8提供全球语言支持,重启系统后重试调试。
  1. 降级C/C++扩展版本
    如果上述方案无效,可尝试将C/C++扩展降级到1.6.0版本,部分场景下高版本扩展的编码兼容处理存在回归问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 13:24:07