PDA

View Full Version : Linux Shared Objects


sybesis
01-07-2007, 11:29 PM
Hi there everyone, I'm new here but well i've been reading a lot of articles.
I'm starting to develop things unix based os but i have difficulties to find some informations on shared objects. This is like dlls of windows.

I found a website but windows frozed so i lost it. I didn't had time to save the page and i can't find it anymore.

all i know is that there may be something like loadlibrary. As i can say, it looks like a shared object in linux is programmed like normal apps but there are functions that are exported.

I didn't find any other information on how to load at runtime these functions. So i could just create function pointers that will redirect on the functions of the shared object. But i can't find any linux code using getprocadress like in windows.

If someone have any information i would just be really happy. By the way, shared objects seems to be really slow compared to static linked object.

Cya

ps: devmaster is really cool :worthy:

Reedbeta
01-07-2007, 11:38 PM
Applications in Linux use a library called "libdl" (dl for dynamic linking) to load and link with the shared libraries. I'm sure there are functions like the Windows LoadLibrary and GetProcAddress. Maybe googling for libdl will find you some useful info. However, generally you won't need to use those functions directly, rather the compiler generates an import library containing the dynamic-linking code and you link statically with that (again just like Windows). Here (http://www-128.ibm.com/developerworks/library/l-shobj/) is a good website that explains the basics of creating and using shared objects with gcc.

Kenneth Gorking
01-08-2007, 02:34 AM
However, in case you do need the functions, here they are:


void *handle = dlopen("somelib.so", RTLD_LAZY);
void *function = dlsym(handle, "someFunction");
// Do some stuff with function
if(dlclose(handle) != 0)
printf("some error");

TheNut
01-08-2007, 04:08 AM
If you want to compile your code as a shared library, the easiest way is to get GCC to do all the dirty work for you (assuming you're using GCC). Export your classes or functions and compile them with argument -fPIC and link them with the argument -shared. Save them as .so (shared object) since this is how Linux names their "dlls". When you next compile your application, GCC will automatically link all the imported functions.

http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

sybesis
01-08-2007, 11:24 PM
Very cool.
I hope i'll be able to show some stuff here.

And yes i use GCC. :happy:

Thank you all