使用GDB打印C ++类对象 [英] Printing C++ class objects with GDB
问题描述
在调试C ++应用程序时,是否有一些默认函数在GDB上打印类似字符串的对象?像这样:toString();
Is there some "default function" to print an object like a string on the GDB when we are debugging C++ applications? Something like: toString();
还是我的班必须实现类似的东西?
Or my class have to implement something like that?
推荐答案
您始终可以使用 print $ c $打印
std :: string
(或其他与此相关的内容) c>命令。但是,在C ++模板容器内部进行挣扎可能并不令人愉快。在最新版本的工具链(通常在大多数用户友好的Linux发行版中作为开发软件包的一部分安装在一起的GDB + Python + Pretty Printers)中,它们会被自动识别并打印(漂亮!)。例如:
You could always have printed std::string
(or anything else for that matter) using print
command. However, struggling with C++ template container internals might not be pleasant. In the recent versions of toolchains (GDB + Python + Pretty Printers that are usually installed together as part of the development packages on most user-friendly Linux distros), those are automatically recognized and printed (pretty!). For example:
$ cat test.cpp
#include <string>
#include <iostream>
int main()
{
std::string s = "Hello, World!";
std::cout << s << std::endl;
}
$ g++ -Wall -ggdb -o test ./test.cpp
$ gdb ./test
(gdb) break main
Breakpoint 1 at 0x400ae5: file ./test.cpp, line 6.
(gdb) run
Starting program: /tmp/test
Breakpoint 1, main () at ./test.cpp:6
6 std::string s = "Hello, World!";
Missing separate debuginfos, use: debuginfo-install glibc-2.16-28.fc18.x86_64 libgcc-4.7.2-8.fc18.x86_64 libstdc++-4.7.2-8.fc18.x86_64
(gdb) next
7 std::cout << s << std::endl;
(gdb) p s
$1 = "Hello, World!"
(gdb)
正如@ 111111指出的那样,请查看 http://sourceware.org/gdb/wiki/STLSupport 了解有关如何自行安装此软件的说明。
As @111111 pointed out, check out http://sourceware.org/gdb/wiki/STLSupport for instructions on how to get this installed yourself.
这篇关于使用GDB打印C ++类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!