C ++,STL,GDB:无法评估函数,可能是内联的 [英] C++, STL, GDB: Cannot evaluate function maybe inlined

查看:164
本文介绍了C ++,STL,GDB:无法评估函数,可能是内联的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用GDB获取地址并从STL容器中打印一对.

I want to be able to get the address and print a single pair from an STL container using GDB.

例如,给定以下玩具程序:

E.g., given the following toy program:

#include <map>

int main() 
{
  std::map<int,int> amap;
  amap.insert(std::make_pair(1,2));

}

我编译为:

g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o main.out main.cpp

然后,当我尝试检查地图的单个元素时,例如:

Then, when I try to examine a single element of map, for example:

p amap.begin())

我得到:

"Cannot evaluate function -- may be in-lined" 

为什么会发生这种情况,我该如何解决?

Why is this happening and how do I work around it?

在Ubuntu 20.04,GCC 9.3.0、2.34中进行了测试.

Tested in Ubuntu 20.04, GCC 9.3.0, 2.34.

推荐答案

这是因为在生成的二进制文件中不存在amap.begin(). C ++模板就是这样工作的:如果您不使用或显式实例化某些模板方法,则该方法不会在生成的二进制文件中生成.

This is because amap.begin() does not exist in resulting binary. This is how C++ templates work: if you don't use or explicitly instantiate some template method it is not generated in resulting binary.

如果您确实想从gdb调用amap.begin(),则必须实例化它.一种方法是实例化std::map的所有方法:

If you do want to call amap.begin() from gdb you have to instantiate it. One way to do it is to instantiate all methods of std::map:

#include <map>

template class std::map<int,int>;

int main()
{
  std::map<int,int> amap;
  amap.insert(std::make_pair(1,2));
}

gdb会话:

(gdb) p amap.begin()
$1 = {first = 1, second = 2}

这篇关于C ++,STL,GDB:无法评估函数,可能是内联的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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