dynamic_cast with dynamically loaded class causes undefined reference
Asked Answered
O

0

6

I have a C++ class whose definition is going to be loaded at run-time through dlopen. I can't get it to link though. I get errors saying there is an "undefined reference to typeinfo".

The relevant part of the code looks like this:

class Interface { ... };
class Impl : public Interface { ... };

Interface *Create() { ... }

// Load shared object around here

Impl *impl = dynamic_cast<Impl*>(Create()); // Undefined reference to typeinfo

I tried adding -rdynamic to my linker command, but this didn't seem to change anything. Is there something I can do about this?

Since I saw many questions about the same error message caused by undefined virtual functions, I should mention that I'm sure this is not my problem.

Also I should add that I'm sure this is related to dynamic_cast, because if I replace the dynamic_cast with a normal (Impl*) cast the code links correctly.

Oersted answered 9/6, 2014 at 13:9 Comment(4)
Please show your dlopen and dlsym code.Radiogram
@KerrekSB: How is that relevant? This happens at link time.Oersted
@Oersted Link time for a .so is when you call dlopen. If you're getting errors when you're invoking ld (through g++), then we'll need to know what options you've used there and to compile.Headpin
@KerrekSB: Errors are indeed happening when ld (through g++) is called. Options passed to g++ are -Wl,-Bstatic -Wl,-Bdynamic along with a few -l options for libraries. I created a minimal test case which I'm compiling/linking with a simple g++ foo.cc and it still causes the same error.Oersted

© 2022 - 2024 — McMap. All rights reserved.