【问题标题】:Undefined symbol, access to a member function with a pointer C++未定义符号,使用指针 C++ 访问成员函数
【发布时间】:2013-03-03 12:08:19
【问题描述】:

我想从 lib 中的一个类中使用指针访问我的主类的成员函数。

我可以访问主类中的公共变量。但是当我尝试访问成员函数(公共)时,我有这个错误:

./Panda: symbol lookup error: libkoala.so: undefined symbol: _ZNK5Panda6getLOLEv

我真的不知道为什么,也许我没有理解 c++ 中的一个概念......

我的主要课程:

class           Panda
{
protected:
  IAssistant*   (*external_creator)();
  IAssistant*   lib;
  void*         dlhandle;

  int           lol;
public:

  int           hello;

  Panda();
  ~Panda();

  int           getLOL() const;
};

Panda::Panda()
{
  if ((dlhandle = dlopen("libkoala.so", RTLD_LAZY)) == NULL)
    exit(-1);
  if ((external_creator = reinterpret_cast<IAssistant* (*)()>(dlsym(dlhandle, "create_assistant"))) == NULL)
    exit(-1);
  lib = external_creator();
  lol = 69;
  hello = 42;
  lib->do_something(this);
}

Panda::~Panda(){
}

int                     Panda::getLOL() const
{
  return lol;
}

界面是这样的:

class IAssistant
{
public:
  virtual void do_something(Panda *) = 0;
};

还有我的库中的类:

class Koala : public IAssistant
{
public:
  void do_something(Panda *);
};

void Koala::do_something(Panda * ptr)
{
  std::cout << ptr->hello; <========= work perfectly 
  std::cout << ptr->getLOL(); <====== doesn't work
}

extern "C"
{
        IAssistant* create_assistant()
        {
                return new Koala();
        }
}

你有什么想法吗?

【问题讨论】:

  • @Luchian 就在第一个代码 sn-p 的底部。
  • 好像你有循环依赖...
  • 为什么你仍然把virtual关键字放在Koala::do_something上,因为你是从IAssistant覆盖它的?

标签: c++ pointers dll member-function-pointers


【解决方案1】:

好的,问题出在编译期间。添加标志 -rdynamic 后,代码可以完美运行。

【讨论】:

    猜你喜欢
    • 2020-06-24
    • 2016-05-12
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 2012-07-07
    相关资源
    最近更新 更多