title: Undefined symbol "typeinfo" with dynamically loaded library
body: <p>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:</p>
  
  <p>base.h:</p>
  
  <pre><code>class base {
  public:
    base();
    virtual ~base();
    virtual int value() = 0;
  };
  </code></pre>
  
  <p>base.cpp:</p>
  
  <pre><code>#include "base.h"
  base::base() {}
  base::~base() {}
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include "base.h"
  #include &lt;dlfcn.h&gt;
  #include &lt;iostream&gt;
  
  int main() {
    void* handle = dlopen("liblib.so", RTLD_NOW);
    if(handle == NULL) std::cout &lt;&lt; dlerror() &lt;&lt; std::endl;
  
    // dlsym, ...
  }
  </code></pre>
  
  <p>lib.cpp:</p>
  
  <pre><code>class derived : public base {
  public:
    derived() {}
    virtual ~derived() {}
    virtual int value() { return 42; }
  };
  
  extern "C" derived* create_object() {
    return new derived();
  }
  </code></pre>
  
  <p>It compiles fine with:</p>
  
  <pre><code>g++ -shared -fPIC lib.cpp -o liblib.so
  g++ base.cpp main.cpp -ldl -o app
  </code></pre>
  
  <p>At runtime however it crashes because of a missing typeinfo symbol</p>
  
  <pre><code>liblib.so: undefined symbol: _ZTI4base
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>$ nm app | grep _ZTI4base
  0000000000601050 V _ZTI4base
  </code></pre>
  
  <p>So why isn't the linker using this definition?</p>
  
  <p>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?</p>