LinuxQuestions.org
Register a domain and help support LQ
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 06-05-2002, 08:00 AM   #1
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Rep: Reputation: 30
How do you load dynamically a library from C?


Hi folks,

i read it everywhere, but i just don't figure out how do you load dynamically a library.

what i know:
in *nix the libraries are object files (.o or .so) which are esentially compiled functions

what i don't know:

how to do something in C / C++ something like:

load("myfunc.o")
int u = myfunc( mydata );


Thanks for any advice!
 
Old 06-05-2002, 08:24 AM   #2
kahuna
Member
 
Registered: Jun 2002
Location: Grand Rapids, MI
Distribution: Redhat, Slackware
Posts: 78

Rep: Reputation: 15
try man dlopen
 
Old 06-05-2002, 11:00 AM   #3
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Original Poster
Rep: Reputation: 30
ouch! thanks man!
 
Old 06-05-2002, 02:36 PM   #4
Hano
Member
 
Registered: Sep 2001
Location: Venezuela, Caracas
Distribution: RedHat 9.0
Posts: 196

Original Poster
Rep: Reputation: 30
Ouch again!! this is my wanna-be module:

testdl.cc
-------------------------------
int test(int n) {
return (n+5);
};

float test2(float f) {
return (f*f);
};
--------------------------------

i compile it with either

icc testdl.cc -o testdl.so -shared

or

gcc testdl.cc -o tesdl.so -shared

Now i want to use it!

DL.cc
----------------------------------------
#include<iostream.h>
#include<dlfcn.h>

int main() {

int (*func1)(int);
float (*func2)(float);
void* handle;


handle = dlopen("/home/charles/prog/testdl.so", RTLD_GLOBAL );

if (!handle) { handle = dlopen ("/home/charles/prog/testdl.so" , RTLD_NOW ); };
if (!handle) { handle = dlopen ("/home/charles/prog/testdl.so" , RTLD_LAZY ); };
if (!handle) { cout << " no handle! "<< dlerror() <<endl; };

func1 =(int*)dlsym( handle , "test" );
func2 =(float*)dlsym( handle , "test2" );

cout << func1(6) << " " << func2 (8.0) << endl;

};

------------------------------
now i compile it with either:

icc DL.cc -o DL -Bdynamic -ldl

or

gcc DL.cc -o DL -rdynamic -ldl



but none works!
1) compiling DL.cc with gcc doesnt even compile! it says:

$ gcc -rdynamic DL.cc -o DL -ldl
DL.cc: In function `int main ()':
DL.cc:38: cannot convert `void *' to `int (*) (int)' in assignment
DL.cc:39: cannot convert `void *' to `float (*) (float)' in assignment


2) icc compiles DL.cc BUT when running it says:

$./DL
segmentation fault

3) testdl.cc (the module) it compiles either with gcc and icc, BUT
if I run DL (compiled with icc which its the only that works) and use testdl.so compiled with (either compilers) but with the -c option (object file) it says:

$gcc -c testdl.cc -o testdl.so -shared
$ ./DL
no handle! /home/charles/prog/testdl.so: ELF file's phentsize not the expected size
Segmentation fault

if i compile without the -c (again, in either compilers)

$ icc testdl.cc -o testdl.so -shared
testdl.cc
$ ./DL
Segmentation fault

so, from here, i dont know what the heck is going on....


'HElp'
 
Old 06-07-2002, 04:04 PM   #5
ddiego
LQ Newbie
 
Registered: Apr 2002
Posts: 2

Rep: Reputation: 0
The problem is in name mangling I beleive. Name mangling is something the C++ compiler does to allow for function overloading, so you can have multiple functions of the same name but different arguments. This is not an issue in C so C code does NOT get name mangled.

If you try compiling using g++ instead of gcc you may get things to work better
But that will only fix the compile/link step

to actually call the function dynamically (AFAIK) you will have to either:
a) make the function a C style function like so :

//in foo.h
extern "C" {
void functionFoo( int arg1, double arg2 );
}

//in foo.cc

void functionFoo( int arg1, double arg2 )
{
//do whatever
}


or:

b) know what the C++ name mangling scheme for the function is, and naturally this is different from compiler vendor to compiler vendor, and may or may not be documented. I know for a fact that the G++ name mangling IS documented - if you do a google search for it you should be good to go.

Hope this helps
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Cannot load .so file from java library path whalesurf Linux - Software 1 07-24-2006 12:43 PM
PHP Warning: Unable to load dynamic library xbaez Linux - Software 3 02-15-2005 07:11 PM
my slack can't load shared library AnhTuan Slackware 6 10-03-2004 10:27 AM
shared object library won't load (libstdc++) sleepylight Linux - General 4 04-27-2004 05:24 PM
SOLVED: nVidia fails to load GLX library Ed-MtnBiker Fedora 0 03-26-2004 05:57 PM


All times are GMT -5. The time now is 07:02 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration