GDB 7.6在使用gcc-4.8和Mac OS 10.9时如何漂亮打印STL?

3
我在我的Mac上使用gdb,想要实现这里描述的漂亮打印效果,但是遇到了困难。我通过macports下载了最新版本的gdb,并使用gcc-4.8。我加载了~/.gdbinit文件并注册了打印机,但每当我调用print myVector时,它都会给出原始输出。你有什么建议吗?非常感谢!

尝试使用@Marco的答案,它有效! - Denny
3个回答

3

我在我的Mac OS X 10.10.3上尝试了一下,它可以工作。GNU gdb (GDB) 7.9.1,g++ --version 配置为:--prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM版本6.1.0(基于LLVM 3.6.0svn)目标:x86_64-apple-darwin14.3.0线程模型:posix clang --version Apple LLVM版本6.1.0(基于LLVM 3.6.0svn)目标:x86_64-apple-darwin14.3.0 - Denny

1

有什么建议吗?

您可以尝试直接从gdb命令行注册漂亮的打印机,绕过.gdbinit文件以缩小问题范围:

ks@ks-comp:~$ gdb -n
GNU gdb (GDB) 7.6.1-ubuntu
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) python
>import sys
>sys.path.insert(0, '/home/ks/stlPrettyPrinter')
>from libstdcxx.v6.printers import register_libstdcxx_printers
>register_libstdcxx_printers (None)
>end
(gdb) 

请注意,我已经检查了位于/home/ks/stlPrettyPrinter文件夹中的漂亮打印机。
ks@ks-comp:~$ ls -a /home/ks/stlPrettyPrinter
.  ..  hook.in  index.html  libstdcxx  Makefile.am  Makefile.in  patch.txt  .svn
ks@ks-comp:~$ 

谢谢您的建议!我已经尝试了,现在没有任何抱怨了,但是仍然得到以下输出:<\br> (gdb) p v $1 = {<_Vector_base<double, std::allocator<double> >> = { _M_impl = {<allocator<double>> = {<new_allocator<double>> = {<No data fields>}, <No data fields>}, _M_start = 0x1004000a0, _M_finish = 0x1004000f0, _M_end_of_storage = 0x1004000f0}}, <No data fields>} 。我使用 g++-4.8 -g -std=c++11 main.cpp 编译了代码。我对 gdb 比较陌生,不知道如何解决问题:/ - user3428018

0
  • 如果你使用的是clang默认的libc++的STL实现,GDB将不知道如何漂亮地打印STL容器。
  • 改用GNU的libstdc++,在gdb中使用STL漂亮打印就可以了。
  • STL漂亮打印程序是由了解STL容器实现细节并与STL实现一起维护的Python程序实现的。
  • libc++不提供符合GDB Python漂亮打印API的STL漂亮打印程序。

由于你从MacPorts获得了你的gcc-4.8MacPorts:使用正确的编译器声称gcc-*默认为libstdc++,所以当使用gcc时,应避免传递-stdlib=选项。


谢谢,使用clang++并传递-stdlib=libstdc++可以工作!但是你有没有办法通过g++-4.8传递它?它不认识标志-stdlib=libstdc++。我通常传递-std=c++11以获取c++11功能。 - user3428018
@user3428018,你也是通过MacPorts安装的gcc-4.8吗? - scottt
是的,我使用了它,因为它支持OpenMP。 - user3428018

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接