Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-05-2002, 07:00 AM   #1
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:

int u = myfunc( mydata );

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

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

Original Poster
Rep: Reputation: 30
ouch! thanks man!
Old 06-05-2002, 01:36 PM   #4
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:
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....

Old 06-07-2002, 03:04 PM   #5
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 );


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


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 11:43 AM
PHP Warning: Unable to load dynamic library xbaez Linux - Software 3 02-15-2005 06:11 PM
my slack can't load shared library AnhTuan Slackware 6 10-03-2004 09:27 AM
shared object library won't load (libstdc++) sleepylight Linux - General 4 04-27-2004 04:24 PM
SOLVED: nVidia fails to load GLX library Ed-MtnBiker Fedora 0 03-26-2004 04:57 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:51 AM.

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