/*
* sample code for loading, calling a method from
* and unloading a shared library on GNU/Linux
*
* cc main.c -o main -ldl
*
* If using c++ compiler the symbol names will be mangled
* So a method called "share" will be "share__Fi" in the
* shared library, Please do a "nm share.sl" to find the
* mangled symbol for the method.
*
* kishan@hackorama.com ( www.hackorama.com ) Feb 2001
*
*/
#include <stdio.h>
#include <dlfcn.h>
int
main( void )
{
const char *sh_lib = "share.sl" ;
const char *method = "share";
void (*fp) (int) = NULL ;
void *handle = dlopen( sh_lib , RTLD_NOW );
if( handle == NULL ){
fprintf( stderr, "\nfailed loading %s\n", sh_lib );
exit(1);
}else{
fp = ( void (*)(int) )dlsym( handle, method );
if( fp == NULL )
fprintf( stderr, "\nfailed getting method %s\n", method);
else
fp( 42 );
if ( dlclose(handle) != 0 )
fprintf( stderr, "\nfailed unloading %s\n", sh_lib );
}
exit(0);
}
syntax highlighted by Code2HTML, v. 0.9