C++共享库中基类的未定义符号错误

Fra*_*ank 4 c++ compilation shared-libraries linker-errors undefined-symbol

我使用以下代码将以下代码编译为共享库g++ -shared ...:

class Foo {
public:
  Foo() {}
  virtual ~Foo() = 0;
  virtual int Bar() = 0;
};

class TestFoo : public Foo {
public:
  int Bar() { return 0; }
};

extern "C" {
  Foo* foo;
  void init() {
    // Runtime error: undefined symbol: _ZN3FooD2Ev
    foo = new TestFoo(); // causes error
  }
  void cleanup() { delete(foo); }
  void bar() { foo->Bar(); }
}
Run Code Online (Sandbox Code Playgroud)

关键是要作为一个简单的暴露我的课(这里只是很小的玩具类为例)的功能C与三个功能API init,cleanupbar.

当我尝试加载共享库(使用dyn.loadin R)时,我收到一个错误:

unable to load shared library 'test.so':
test.so: undefined symbol: _ZN3FooD2Ev
Run Code Online (Sandbox Code Playgroud)

所以,它似乎无法找到Foo构造函数.我做错了什么,如何解决?

更新:谢谢,jbar!所以它是Foo de structor.我可以从错误信息中的神秘符号中知道这个:_ZN3FooD2Ev?请问DFooD代表的析构函数?

Duc*_*uck 13

更新:所以它是Foo析构函数.我可以从错误消息中的神秘符号中知道这个:_ZN3FooD2Ev吗?FooD中的D代表析构函数吗?

您可以使用程序c ++ filt.

所以c ++ filt _ZN3FooD2Ev返回"Foo :: ~Foo()".

  • +1非常感谢你介绍我c ++ filt! (3认同)

小智 8

我们不能声明纯虚析构函数.即使虚拟析构函数被声明为纯粹的,它也必须为析构函数实现一个空体(至少).