先说结论:
在 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 下只有两个替代:
- 使用 LLDB + 自己写 formatters(麻烦,不如 qt5printers 稳定)
- 改用 Qt Creator 调试(它内置 natvis 风格显示,但编辑器不是 Cursor)
结论:
在 Cursor + Ubuntu + Qt5.15 下,老老实实用 qt5printers.py 是唯一正确、最简单、最稳的方案,别折腾 natvis 了,不支持。
需要我帮你把真实 Qt 路径直接填好,给你一份复制粘贴就能用的最终版吗?你只需要发我:
bash
find ~/Qt5.15.0 -name "qt5printers.py"
的输出即可。