如下图所示:用VScode调试C++代码时,无法显示我们想要的值。
解决方法:编辑launch.json,加入如下设置:
"setupCommands": [
{
"description": "Test",
"text": "python import sys;sys.path.insert(0, '/usr/share/gcc-8/python');from libstdcxx.v6.printers import register_libstdcxx_printers;register_libstdcxx_printers(None)",
"ignoreFailures": false
},
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
注意:这里“/usr/share/gcc-8/python”这个路径根据每个人安装的路径不同可能会不一样的。我们得根据实际路径进行填写
因为我是用VScode来进行远程调试,所以整个launch.json如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "/home/cjc/FUCK_TCP/FUCK_TCP/main",
"args": [],
"stopAtEntry": false,
"cwd": "/home/cjc/FUCK_TCP/FUCK_TCP",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Test",
"text": "python import sys;sys.path.insert(0, '/usr/share/gcc-8/python');from libstdcxx.v6.printers import register_libstdcxx_printers;register_libstdcxx_printers(None)",
"ignoreFailures": false
},
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ 生成活动文件",
"miDebuggerPath": "/usr/bin/gdb",
"miDebuggerServerAddress":"192.168.1.4:2000"
}
]
}
重新按F5启动调试,我们发现可以正常显示string,vector等STL容器的值了
参考文章:
《How can i see the value of elements in STL containers or arrays in debug #1414》
《C++ Debug Window showing "<incomplete type> for string variable》