-   Programming (
-   -   C++ and dynamic library loading (

Osiris990 11-06-2007 12:44 PM

C++ and dynamic library loading
'tis me once again with another C++ problem. =/

So I'm trying to figure out how to load dynamic libraries, and I'm reading a tutorial ( that's telling me to static_cast to the class type I want after I load the function symbol, but the problem is it's giving me the error:

error: invalid static_cast from type 'void*' to type 'Module* ()()'
and I don't understand why. Here's the function I have that reads the file in and tries to load the module:

And here is the current prototype for the Module class (not much there):



Osiris990 11-06-2007 03:34 PM

Well, I found a way to get it to compile... Problem is, it segfaults when it runs, so no dice. Replacing the static_cast<> in the above with

Module* module = ((Module*(*)())(mkr_ptr))();
made it compile, but once again it just segfaulted on me. When I ran it through GDB, it shows me an error at 0x00000000, which if my assumptions are correct, means it's a null pointer, though I'm not 100% sure. Thoughts/ideas?

Thanks again,

keratos 11-06-2007 04:01 PM

for a start you have a typo: cut n paste this:

Module *module = static_cast<Module *()>(mkr_ptr)();

But I dont think that will work because the Module class function you are calling (CreatePtr) returns a pointer to a void and you cannot convert a "nothing" pointer to a Module pointer. That is why the "invalid static..." error is being generated.

Try setting up a typedef that is a pointer to a Module and use the typedef as the prototype for CreatePtr. The typedef should then be castable to a *Monitor. !!

ntubski 11-06-2007 07:21 PM

You have overlooked the fact that dlsym works on C functions, but C++ has this name-mangling scheme to allow things like overloading, namespaces, and class stuff to work. I see in your code that you don't check the return value of dlsym :tisk:, it's returning NULL because it can't find a symbol called "CreatePtr". g++ gives it a different name, something like "CreatePtr__6Module". Also since you made CreatePtr a member method of Module you need a Module object before you can call CreatePtr!

What you probably want is CreatePtr to be a stand alone function with C linkage to avoid name-mangling, and it might as well return a pointer to a Module:

extern "C" Module* CreatePtr() {
  return new Module;

Also you might want to call it CreateModule...


This link might be handy:

Osiris990 11-07-2007 12:40 PM

Woo! I've got it all hashed out and working now. Thanks a lot, guys! When I'm finished with the core program, I'll post it on here and let you guys play around with it a little bit. :D

ta0kira 11-08-2007 11:07 AM

The first thing you always need to check when dynamic loading, regardless of if you have problems, is the return value of dlopen and dlsym. If they return NULL and you print out dlerror that will tell you the problem, which in this case would probably be something like "symbol not found". That will make things quite a bit easier when debugging.

All times are GMT -5. The time now is 09:55 PM.