|
#include <stdio.h> #include <dlfcn.h>
#define null 0
///* declarations used for dynamic linking support routines */
//extern void *dlopen (const char *, int);
//extern void *dlsym (void *, const char *);
//extern int dlclose (void *);
//extern char *dlerror (void);
int main(int argc, char **argv) { printf("load so demo... \n");
// open the library
printf("opening libhello.so.1.0...\n"); void* handler = dlopen("libhello.so.1.0", RTLD_LAZY); if(!handler) { printf("cannot open library: %s \n", dlerror()); return 1; } // load the symbol
printf("loading the symbol 'debug_print'...\n"); typedef void (*symbol_t)(); // reset dlerror
symbol_t debug_print = null; debug_print = (symbol_t) dlsym(handler, "debug_print"); if(!debug_print) { printf("cannot load the symbol 'debug_print': %s \n", dlerror()); dlclose(handler); return 1; } // using the symbol
printf("using the symbol 'debug_print'...\n"); debug_print(); // close the library
printf("close the library...\n"); dlclose(handler); return 0;
}
|