简体   繁体   中英

Undefined symbol “typeinfo” with dynamically loaded library

I'm trying to build a shared library using gcc 4.6 on Linux, which is dynamically loaded. As described in many articles on the web as well as in previous questions, I provide c-style factory methods in the library to create and destroy objects. The code - in minimal form - looks like this:

base.h:

class base {
public:
  base();
  virtual ~base();
  virtual int value() = 0;
};

base.cpp:

#include "base.h"
base::base() {}
base::~base() {}

main.cpp:

#include "base.h"
#include <dlfcn.h>
#include <iostream>

int main() {
  void* handle = dlopen("liblib.so", RTLD_NOW);
  if(handle == NULL) std::cout << dlerror() << std::endl;

  // dlsym, ...
}

lib.cpp:

class derived : public base {
public:
  derived() {}
  virtual ~derived() {}
  virtual int value() { return 42; }
};

extern "C" derived* create_object() {
  return new derived();
}

It compiles fine with:

g++ -shared -fPIC lib.cpp -o liblib.so
g++ base.cpp main.cpp -ldl -o app

At runtime however it crashes because of a missing typeinfo symbol

liblib.so: undefined symbol: _ZTI4base

In the previous questions I found here, this error was usually due to either some missing "= 0;" or a missing definition of a virtual function. In the example above however, base::value is pure virtual and the destructor has a definition. Strangely enough nm reports _ZTI4base as definied in app:

$ nm app | grep _ZTI4base
0000000000601050 V _ZTI4base

So why isn't the linker using this definition?

The only way I found so far to get the code working is to implement constructur and destructor in the header file. After doing this however, the corresponding symbols for base are reported in liblib.so by nm and totally disappear from app, which probably means, that their definitions were compiled into the library and not into app, which is not what I wanted to achieve. Has anyone an idea how to get the above working without doing this?

链接程序时需要-rdynamic选项,导出其符号并使其可用于加载dlopen()库。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM