LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2007, 12:44 PM   #1
Osiris990
Member
 
Registered: Oct 2005
Location: Dallas, TX
Distribution: Slackware 12
Posts: 49

Rep: Reputation: 15
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 (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* ()()'
and I don't understand why. Here's the function I have that reads the file in and tries to load the module:
http://pastebin.is-l33t.net/index.php?show=157

And here is the current prototype for the Module class (not much there):
http://pastebin.is-l33t.net/index.php?show=158

Thoughts?

Thanks,
Shane
 
Old 11-06-2007, 03:34 PM   #2
Osiris990
Member
 
Registered: Oct 2005
Location: Dallas, TX
Distribution: Slackware 12
Posts: 49

Original Poster
Rep: Reputation: 15
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?

Thanks again,
Shane
 
Old 11-06-2007, 04:01 PM   #3
keratos
Member
 
Registered: May 2007
Location: London, UK.
Distribution: Major:FC8. Others:Debian;Zenwalk; Arch; Slack; RHEL.
Posts: 544

Rep: Reputation: 30
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. !!
 
Old 11-06-2007, 07:21 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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;
}
Also you might want to call it CreateModule...

EDIT

This link might be handy: http://www.faqs.org/docs/Linux-mini/C++-dlopen.html

Last edited by ntubski; 11-06-2007 at 07:38 PM.
 
Old 11-07-2007, 12:40 PM   #5
Osiris990
Member
 
Registered: Oct 2005
Location: Dallas, TX
Distribution: Slackware 12
Posts: 49

Original Poster
Rep: Reputation: 15
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.
 
Old 11-08-2007, 11:07 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Base Address of Dynamic Library Damaged Soul Programming 11 12-13-2010 01:28 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
Linking a static library to a dynamic one delta4s Programming 2 09-28-2006 01:24 AM
error loading dynamic library with Java trutnev Programming 4 06-01-2005 03:33 PM
dynamic library versioning kev82 Linux - General 9 12-13-2003 09:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration