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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)