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

Linux下VSCode C++ GDB调试器不显示指针动态类型如何解决

VSCode调试C++多态指针不显示动态类型问题

运行环境

运行环境为Linux系统下的VSCode,版本信息如下:

λ code --version
1.62.2
3a6960b964327f0e3882ce18fcebd07ed191b316
x64

问题复现

如下C++代码中的变量myBase:

#include <iostream>

class MyBase {
    public:
        virtual int myMethod() = 0;
};

class MyDerived1 : public MyBase {
    public:
        virtual int myMethod() { return 1; }
};

class MyDerived2 : public MyBase {
    public:
        virtual int myMethod() { return 2; }
};

int main() {
    MyBase *myBase;
    MyDerived1 myDerived1;
    MyDerived2 myDerived2;
    myBase = &myDerived1;
    std::cout << myBase->myMethod() << std::endl;
    myBase = &myDerived2;
    std::cout << myBase->myMethod() << std::endl;
}

在VSCode调试器中始终被显示为MyBase类型,如下图所示:
调试界面截图
预期它会根据程序暂停的位置,显示为MyDerived1或者MyDerived2类型。QtCreator和原生GDB都可以正确显示该动态类型,GDB输出如下:

(gdb) p myBase
$7 = (MyBase *) 0x7fffffffdce0
# 单步执行
(gdb) p myBase
$8 = (MyDerived1 *) 0x7fffffffdbd0
# 单步执行
(gdb) p myBase
$9 = (MyDerived2 *) 0x7fffffffdbd8

已尝试操作

已经在.gdbinit中配置了set print object on,并确认VSCode已读取该配置,也尝试过在VSCode的调试控制台中手动执行该命令,仍未生效。GDB启动日志如下:

=thread-group-added,id="i1"
GNU gdb (Ubuntu 10.1-2ubuntu2) 10.1.90.20210411-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
=cmd-param-changed,param="auto-load safe-path",value="/"
=cmd-param-changed,param="disassembly-flavor",value="intel"
=cmd-param-changed,param="print max-symbolic-offset",value="1"
=cmd-param-changed,param="pagination",value="off"
=cmd-param-changed,param="print thread-events",value="off"
=cmd-param-changed,param="confirm",value="off"
=cmd-param-changed,param="print asm-demangle",value="on"
=cmd-param-changed,param="print object",value="on"
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.

解决方法

该问题是C/C++扩展(cpptools)的默认配置未开启动态类型展示导致,即便GDB侧已经打开print object开关,VSCode调试前端仍然会默认展示静态类型,按以下步骤修改即可:

  1. 打开项目下.vscode/launch.json文件
  2. 在你使用的C++调试配置项中,添加"showDynamicType": true配置,示例如下:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "showDynamicType": true, // 添加这一行
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  1. 保存修改后重启调试会话,变量窗口即可正常显示基类指针指向的实际派生类型。
    如果修改后仍不生效,请将C/C++扩展升级到1.7.0及以上版本,该版本开始正式支持showDynamicType配置项。

内容的提问来源于stack exchange,提问作者Daniel Näslund

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 04:24:03