c++ - 抽象类使用shared_ptr时如何在nm或objdump中找到函数符号?

标签 c++ compilation debug-symbols objdump nm

我将 shared_ptr 用于抽象类 ABC。 ABCImpl类是ABC的实现。 abc_ptr 是一个shared_ptr 指向一个ABCImpl 对象。在调用函数中,abc_ptr 将调用 ABC 类中的成员函数(func_in_ABC)之一。编译成功。但是当我使用 nm 或 objdump 时,我只能看到 abc_ptr 的符号。调用函数中的 func_in_ABC() 没有显示符号。

任何人都知道为什么,或者我如何在调用函数中获得 func_in_ABC() 符号的输出?

代码如下: 在 ABC.h 中:

#include <boost/shared_ptr.hpp>

class ABC
{
    public:
        virtual void func_in_ABC(const int param) = 0;
};

typedef boost::shared_ptr<ABC> ABCPtr;
ABCPtr get_ABC_ptr();

在 ABCImpl.h 中:

#include "ABC.h"

class ABCImpl : public 
{
    public:
        ABCImpl() {}
        void func_in_ABC(const int param);
    private:
        int data;
};

在 ABCImpl.cpp 中:

#include "ABCImpl.h"

ABCPtr get_ABC_ptr()
{
        return ABCPtr(new ABCImpl());
}

void ABCImpl::func_in_ABC(const int param)
{
    data = param;
}

在调用函数 D.cpp 中:

#include "D.h"
#include "ABC.h"

void D::call_ABC()
{
    ABCPtr abc_ptr = get_ABC_ptr();
    abc_ptr->func_in_ABC(100);
}

nm 的 D.o 输出:

         U _Unwind_Resume
         U get_ABC_ptr()
0000000000000000 T D::call_ABC()
0000000000000000 W boost::shared_ptr<ABC>::operator->() const
0000000000000000 r boost::shared_ptr<ABC>::operator->() const::__PRETTY_FUNCTION__
         U __assert_fail
         U __gxx_personality_v0

如果我在 ABC.h 中更改 func_in_ABC 的定义,则 D.cpp 的编译将失败。我认为它会在编译 D.o 时检查类 ABC 的定义。但为什么我在 caller 找不到映射到 ABC 中定义的符号?

最佳答案

由于 func_in_ABC 是一个虚函数,您实际上不需要符号名称来调用它。您只需要该特定虚拟函数的虚拟表偏移量。

如果您使 func_in_ABC 成为非虚拟的,您应该会在 nm 输出中看到该符号。

关于c++ - 抽象类使用shared_ptr时如何在nm或objdump中找到函数符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6805923/

相关文章:

Xcode 4 Instruments 不显示源代码行

macos - 如何符号化 Electron 崩溃日志

c++ - Boost::Thread/C++11 std::thread,想要在条件下唤醒工作线程

c++ - 隐藏具有相似原型(prototype)的非虚拟方法没有警告 (G++ 4.4)

c++ - 将 bool /整数数组传递给应该改变它的函数

ios在不支持ac3的情况下编译ffmpeg

scala - 将 Java 对象(实际上是 Scala Map)转换为 Scala Map

c - 如何获得 Debian 附带的程序的调试符号?

c++ - 统一迭代 std::vector<T> 和 std::vector<unique_ptr<T>>

c++ - 使用列表进行动态结构分配