使用 GDB 打印 C++ 类对象

Printing C++ class objects with GDB(使用 GDB 打印 C++ 类对象)

本文介绍了使用 GDB 打印 C++ 类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们调试 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 打印 std::string (或其他任何东西)命令.然而,与 C++ 模板容器内部的斗争可能并不令人愉快.在最新版本的工具链(GDB + Python + Pretty Printers,通常作为开发包的一部分安装在大多数用户友好的 Linux 发行版上)中,它们会被自动识别和打印(漂亮!).例如:

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++ 类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 GDB 打印 C++ 类对象

基础教程推荐