/* NOTE: compile this with the -ldl flag! */

#include <stdlib.h>

#include <stdio.h>

#include <dlfcn.h> /* for dlopen */

int main ( )

{

/* Find the library '/lib/libm.so.6' and load it dynamiclly into

the program; RTLD_NOW means, that if the loader can't

resolve all undefined symbols, it will fail */

void * handle = dlopen ( "/lib/libm.so.6" , RTLD_NOW ) ;

if ( handle == NULL )

{

fprintf ( stderr , "ERROR: %s

" , dlerror ( ) ) ;

return - 1 ;

}

/* Now we can search for symbols in the loaded library

with the 'handle' variable. */

/* dlsym returns a void pointer. We now need to cast it to

what we expect it to be in this case a function pointer. */

double ( * cos_function ) ( double ) = dlsym ( handle , "cos" ) ;

if ( cos_function == NULL )

{

fprintf ( stderr , "Could not find symbol or symbol is NULL!

" ) ;

}

printf ( "%f

" , cos_function ( 20.0 ) ) ;

return 0 ;