/* load the math library, and print the cosine of 2.0: */ /* compile with gcc -rdynamic -o foo foo.c -ldl */ #include #include int main(int argc, char **argv) { void *handle; double (*cosine)(double); char *error; handle=dlopen ("/lib/libm.so.5", RTLD_LAZY); if (!handle) { fputs(dlerror(),stderr); exit(1); } cosine=dlsym(handle, "cos"); if ((error = dlerror()) != NULL) { fputs(error, stderr); exit(1); } printf ("%f\n", (*cosine)(2.0)); dlclose(handle); return 0; }