Dynamic load of libraries

i’ve taken that from the advanced forum:

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
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.



Yes,it is possible.

and how do you do that ? (that was intrinsec in my question)

Originally posted by jide:
and how do you do that ? (that was intrinsec in my question)

man dlopen:

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

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);

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

", error);

              printf ("%f

", (*cosine)(2.0));

   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

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.

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.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.