用gdb调试c ++程序 [英] debugging c++ programs with gdb

查看:126
本文介绍了用gdb调试c ++程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们调试C ++应用程序时,是否有一些缺省函数在GDB上打印对象,如字符串?如:toString();



或者我的类必须实现类似的东西?

解决方案

您可以使用 print 打印 std :: string (或其他任何东西) c>命令。然而,在C ++模板容器内部挣扎可能不愉快。在最近版本的工具链中(GDB + Python + Pretty Printers通常作为大多数用户友好的Linux发行版中的开发包的一部分安装在一起),这些工具链会自动识别并打印(非常漂亮!)。例如:

  $ 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 $
(gdb)运行
启动程序:/ tmp / test

断点1,main(
断点1在0x400ae5处:文件./test.cpp, )at ./test.cpp:6
6 std :: string s =Hello,World!;
缺少单独的debuginfos,请使用: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 了解如何自行安装它的说明。


Is there some "default function" to print a 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?

解决方案

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) 

As @111111 pointed out, check out http://sourceware.org/gdb/wiki/STLSupport for instructions on how to get this installed yourself.

这篇关于用gdb调试c ++程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆