PDA

View Full Version : dynamic load of libraries



jide
12-15-2001, 07:52 AM
i've taken that from the advanced forum:

Syslock
Contributor posted 12-14-2001 01:26 PM
--------------------------------------------------------------------------------
I want to dynamically load opengl32.dll using LoadLibrary and
then load its functions with GetProcAddress, but I have a couple
questions.
Will dynamically loading work the same for different OS,
such as Win 95/98/NT/ME/2k/XP?

Can we load a *.so lib dynamically under Linux ? -i don't want to load opengl libs, i don't think it's a good way.

And how to unload them ?

i think that's may be interressant to load some objects.

thanx

JD

FoxDie
12-15-2001, 11:32 AM
Yes,it is possible.

jide
12-16-2001, 03:20 AM
and how do you do that ? (that was intrinsec in my question)

rts
12-16-2001, 11:27 AM
Originally posted by jide:
and how do you do that ? (that was intrinsec in my question)

man dlopen:


NAME
dlclose, dlerror, dlopen, dlsym - Programming interface to
dynamic linking loader.


...

EXAMPLE
Load the math library, and print the cosine of 2.0:




#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;

handle = dlopen ("/lib/libm.so", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}

cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}

printf ("%f\n", (*cosine)(2.0));
dlclose(handle);
}

If this program were in a file named "foo.c", you would
build the program with the following command:

gcc -rdynamic -o foo foo.c -ldl

zerodeux
12-16-2001, 10:23 PM
Right, dlopen() will do the job. But as you stated, this is a _bad_ idea to load any standard library via dlopen(), just trust the runtime linker ! It prevents you from maintaining tedious function pointers, let the user play with runtime linking via standard mecanism (man ld.so).

If you think that the only reason why you woul do this is when an application wants to use GL only when it's available, it is also a bad idea : simply compile the GL dependent part as a DLL (.so) linked against GL, then dlopen() it. The runtime linker will do the job for you, and you'll only have to dlsym() your own symbols.

jide
12-17-2001, 02:19 AM
i just want to load/unload some datas ( i think i could load 3d models like this). that's may not be a good idea; but it is just for test.