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?
You need the -rdynamic
option when linking the program, to export its symbols and make them available to libraries loaded with dlopen()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With