/* * ISA 563, Spring 2011 * Copyleft by Muhammad Abdulla */ #include #include #include #include /* * dynmath.c -- demonstrates dynamic loading of library functions */ int main(int argc, char **argv) { void *handle; double (*cosine)(double); double (*sine)(double); char *error; char *libpath = "/lib/libm.so.6"; double angle = M_PI / 3; if ( argc > 1 ) { libpath = argv[1]; } if ( argc > 2 ) { angle = atof(argv[2]); } handle = dlopen (libpath, RTLD_LAZY); if (!handle) { fputs (dlerror(), stderr); exit(1); } cosine = dlsym(handle, "cos"); if ((error = dlerror()) != NULL) { fputs(error, stderr); exit(1); } sine = dlsym(handle, "sin"); if ((error = dlerror()) != NULL) { fputs(error, stderr); exit(1); } printf ("sin(%g) = %g\n", angle, (*sine)(angle)); printf ("cos(%g) = %g\n", angle, (*cosine)(angle)); dlclose(handle); }