ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So I'm trying to figure out how to load dynamic libraries, and I'm reading a tutorial (http://www.linuxjournal.com/article/3687) 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:
Code:
error: invalid static_cast from type 'void*' to type 'Module* ()()'
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
Code:
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?
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. !!
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 , 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:
Code:
extern "C" Module* CreatePtr() {
return new Module;
}
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.
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.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.