答案 0 :(得分:21)
从SVN获取python查看器
svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
将以下内容添加到~/.gdbinit
python
import sys
sys.path.insert(0, '/path/to/pretty-printers/dir')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
然后打印就可以了:
std::map<int, std::string> the_map;
the_map[23] = "hello";
the_map[1024] = "world";
在gdb中:
(gdb) print the_map
$1 = std::map with 2 elements = { [23] = "hello", [1024] = "world" }
要返回旧视图,请使用print /r
(/r
是原始视图)。
答案 1 :(得分:2)
libstdcxx_printers包含在最新版本的GCC中,所以如果您使用的是GCC 4.5或更高版本,那么您不需要做任何事情,相当打印Just Works。
(gdb) p v
$1 = std::vector of length 3, capacity 3 = {std::set with 3 elements = {
[0] = 1, [1] = 2, [2] = 3}, std::set with 2 elements = {[0] = 12,
[1] = 13}, std::set with 1 elements = {[0] = 23}}
(gdb) p v[1]
$2 = std::set with 2 elements = {[0] = 12, [1] = 13}
要禁用漂亮的打印,请使用p/r
或print/r
来获取“原始”输出。