我使用以下代码将以下代码编译为共享库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(); }
}
重点是将我的类(这里只是最小的玩具类作为示例)的功能公开为具有三个函数、和的简单CAPI 。initcleanupbar
当我尝试加载共享库(使用dyn.loadin R)时,出现错误:
unable to load shared library 'test.so':
test.so: undefined symbol: _ZN3FooD2Ev
所以,它似乎找不到Foo构造函数。我做错了什么,如何解决?
更新:谢谢,jbar!所以它是Foo 析构函数。我是否可以从错误消息中的神秘符号中知道这一点:_ZN3FooD2Ev?DinFooD代表析构函数吗?