LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How do you load dynamically a library from C? (http://www.linuxquestions.org/questions/programming-9/how-do-you-load-dynamically-a-library-from-c-22640/)

Hano 06-05-2002 08:00 AM

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!

kahuna 06-05-2002 08:24 AM

try man dlopen

Hano 06-05-2002 11:00 AM

ouch! thanks man!

Hano 06-05-2002 02:36 PM

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'

ddiego 06-07-2002 04:04 PM

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


All times are GMT -5. The time now is 01:05 AM.