漂亮的地图打印机会抛出类型错误
我已经使用 http://wiki.eclipse.org/CDT/ 配置了漂亮的打印机用户/FAQ#How_can_I_inspect_the_contents_of_STL_containers.3F。它成功地适用于矢量和其他容器。但是我无法检查地图,如下例所示:
#include <map>
#include <iostream>
using namespace std;
int main ()
{
map <int, string> mapIntToString;
map <int, int> mapInt2;
mapIntToString.insert (map <int, string>::value_type (3, "Three"));
mapInt2.insert (map <int, int>::value_type (3, 4));
return 0;
}
使用 gdb 打印时出现以下错误:
(gdb) p mapInt2
$1 = std::map with 1 elementsTraceback (most recent call last):
File "/home/myuser/opt/gdb_printers/python/libstdcxx/v6/printers.py", line 422, in children
rep_type = find_type(self.val.type, '_Rep_type')
File "/home/myuser/opt/gdb_printers/python/libstdcxx/v6/printers.py", line 45, in find_type
raise ValueError, "Cannot find type %s::%s" % (str(orig), name)
ValueError: Cannot find type std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::_Rep_type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用什么编译器(以及哪个版本)来构建测试源?
我猜这不是
g++
的最新版本。这是我使用g++ 4.4.3-4ubuntu5
得到的结果:更新:
我看到了问题。您引用的说明不正确。
特别是,指令建议:
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
,但问题是Python代码到达libstdc++
内部结构,因此必须匹配这些内部结构(这就是漂亮打印机是 GCC 的一部分而不是 GDB 的一部分的原因,这是事实bruce.banner 抱怨)。当您执行新的
svn co ...
时,您获取了一份 Python 代码副本,该代码不再与您的libstdc++
内部匹配,这就是是什么给你带来了问题。特别是,
svn log
显示此处添加了find_type
:这比
gcc-4.4.3
晚很多。然后,您想要做的是获得与您的 libstdc++ 版本相匹配的漂亮打印机,如下所示:除了上述命令将不起作用,因为 gcc 4.4.3 早于漂亮的打印机。
不管怎样,std::map 的实现(以及 STL 内部的大部分其他部分)在 4.4.3 和 4.6 之间没有改变,并且这个命令确实有效:
What compiler (and which version) did you use to build your test source?
I am guessing it wasn't a recent version of
g++
. Here is what I get withg++ 4.4.3-4ubuntu5
:Update:
I see the problem. The instructions you've referenced are incorrect.
In particular, the instructions suggest:
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
, but the problem is that the python code reaches intolibstdc++
internals, and therefore must match these internals (this is the reason the pretty printers are part of GCC and not part of GDB, a fact bruce.banner complained about).When you did a fresh
svn co ...
, you picked up a copy of python code that no longer matches yourlibstdc++
internals, and that's what is causing you problems.In particular,
svn log
shows thatfind_type
was added here:That's much later than
gcc-4.4.3
. What you want to do then, is to get pretty printers that match your version oflibstdc++
, like so:Except above command will not work, because gcc 4.4.3 predates the pretty printers.
No matter, the implementation of
std::map
(and much of the rest of STL internals) has not changed between 4.4.3 and 4.6, and this command does work:在我的系统上,类型
_Rep_type
不是std::map
的公共类型(它是私有 typedef),因此脚本试图找出一个._M_t
变量的类型,其类型为_Rep_type
...我尝试过:
然后在
gdb
中我可以打印密钥3
像这样(遵循下面链接的脚本中的打印功能):其中 /
std::map
中的_M_t
是_Rb_tree
类型,但该类型在地图中不是公开的(您可以在map
标头中看到这一点,特别是不确定这是否有一点帮助,基本上您正在加载的漂亮打印 python 函数似乎存在问题。
我刚刚尝试添加到
.gdbinit< code> 来自 来自 yolinux.com 的 GNU GDB 调试器命令备忘单 的内容(我用 google 搜索)为了
gdb Pretty print
),这样我就得到了合理的输出:On my system the type
_Rep_type
is not public type ofstd::map
(it's private typedef) so the script is trying to figure out a type of<yourmap>._M_t
variable which is of type_Rep_type
...I tried:
then in
gdb
I can print the key3
like this (following the print function from the script I linked below):Where the
_M_t
instd::map
is of_Rb_tree
type but the type is not public in map (you can see that in yourmap
header, specifically<path/to/std-headers/dir/bits/stl_map.h
header file.Not sure if that helps one bit, basically there seems to be an issue with the pretty print python function that you are loading.
I've just tried adding to
.gdbinit
stuff from GNU GDB Debugger Command Cheat Sheet from yolinux.com (I googled forgdb pretty print
) and with that I get reasonable output: