-   Programming (
-   -   How do you load dynamically a library from C? (

Hano 06-05-2002 07: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:

int u = myfunc( mydata );

Thanks for any advice!

kahuna 06-05-2002 07:24 AM

try man dlopen

Hano 06-05-2002 10:00 AM

ouch! thanks man!

Hano 06-05-2002 01:36 PM

Ouch again!! this is my wanna-be module:
int test(int n) {
return (n+5);

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

i compile it with either

icc -o -shared


gcc -o -shared

Now i want to use it!

int main() {

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

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

if (!handle) { handle = dlopen ("/home/charles/prog/" , RTLD_NOW ); };
if (!handle) { handle = dlopen ("/home/charles/prog/" , 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 -o DL -Bdynamic -ldl


gcc -o DL -rdynamic -ldl

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

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

2) icc compiles BUT when running it says:

segmentation fault

3) (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 compiled with (either compilers) but with the -c option (object file) it says:

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

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

$ icc -o -shared
$ ./DL
Segmentation fault

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


ddiego 06-07-2002 03: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 );


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


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 12:28 PM.