Curso调试Qt:GDB + Qt 官方 qt5printers.py + .gdbinit

先说结论:
在 Ubuntu 24.04 + GDB 下,不能用 natvis,natvis 只对 Windows + MSVC 调试器有效。

Linux / GDB 只认 Python pretty printers(就是之前给你的 qt5printers.py 那套),这是唯一原生、稳定、官方支持的方案。


为什么不能用 natvis

  • .natvis 是微软为 Visual Studio / VS Code + Windows 调试器(CDB/VS Debugger)设计的变量可视化格式
  • GDB(Linux 默认调试器)不支持 natvis
  • Cursor/VS Code 在 Linux 上底层还是 GDB,所以也不支持

你在 launch.json 里写 visualizerFile 这类配置,在 Ubuntu 上完全无效


那 Ubuntu + Qt5.15 最好的方案是什么?

就是我上一轮给你的这套:
GDB + Qt 官方 qt5printers.py + .gdbinit

这是:

  • 最稳定
  • 官方自带
  • 一次配置永久生效
  • QString / QList / QMap / QVariant 全都能正常显示

比任何第三方插件都靠谱。


我给你精简成「最终可用版」(Ubuntu 24.04 + Qt5.15.0)

1)~/.gdbinit

python 复制代码
python
import sys
sys.path.insert(0, "/home/你的用户名/Qt5.15.0/Tools/QtCreator/share/qtcreator/debugger")
from qt5printers import register_printers
register_printers(gdb.current_objfile())
end

set print pretty on
set print object on

把路径换成你真实路径。

2)launch.json 正确配置(Linux GDB 专用)

json 复制代码
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/Debug/bin/RTMG_APP_XGW",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                },
                {
                    "description": "Load Qt5 pretty printers",
                    "text": "source /home/david/MySoft/MyCode/MG_APPRT_Console/.vscode/.gdbinit",
                    "ignoreFailures": false
                }
            ]
        }

    ]
}

注意:type 是 cppdbg,不是 cppvsdbg,后者是 Windows 用的。


如果你实在想要"类似 natvis"的体验

在 Linux 下只有两个替代:

  1. 使用 LLDB + 自己写 formatters(麻烦,不如 qt5printers 稳定)
  2. 改用 Qt Creator 调试(它内置 natvis 风格显示,但编辑器不是 Cursor)

结论:

在 Cursor + Ubuntu + Qt5.15 下,老老实实用 qt5printers.py 是唯一正确、最简单、最稳的方案,别折腾 natvis 了,不支持。

需要我帮你把真实 Qt 路径直接填好,给你一份复制粘贴就能用的最终版吗?你只需要发我:

bash 复制代码
find ~/Qt5.15.0 -name "qt5printers.py"

的输出即可。

相关推荐
Dxy12393102166 小时前
Python 使用正则表达式将多个空格替换为一个空格
开发语言·python·正则表达式
故事和你918 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
techdashen9 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
星空椰9 小时前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript
忒可君9 小时前
C# winform 自制分页功能
android·开发语言·c#
Rust研习社9 小时前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
leaves falling10 小时前
C++模板进阶
开发语言·c++
坐吃山猪10 小时前
Python27_协程游戏理解
开发语言·python·游戏
gCode Teacher 格码致知10 小时前
Javascript提高:小数精度和随机数-由Deepseek产生
开发语言·javascript·ecmascript
椰猫子11 小时前
Javaweb(Filter、Listener、AJAX、JSON)
java·开发语言