SWAT /

C Dynamic Library Loading

Reading

Outdoors

Games

Hobbies

LEGO

Food

Code

Events

Nook

sidebar

C Dynamic Library Loading

C/C++ Dynamic Library Loading

From the man page of dlopen:

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

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

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

        cosine = (double (*)(double)) dlsym(handle, "cos");
        if ((error = dlerror()) != NULL)  {
            fputs(error, stderr);
            exit(1);
        }

        printf ("%f\n", (*cosine)(3.1415926));
        dlclose(handle);
	return 0;
    }

Compile it like so:

  gcc -rdynamic -o test test.c -ldl
Recent Changes (All) | Edit SideBar Page last modified on July 31, 2007, at 10:23 AM Edit Page | Page History