PROMO SELL

Tuesday, March 08, 2005

Sample Code - Using shared libs on UNIX

Sample C code for :

1. Creating a shared library
2. Runtime loading of a shared library
3. Calling a method from the loaded library


On GNU/Linux :

share.c

/*
* sample code for creating a shared library on GNU/Linux
*
* cc -fPIC -c share.c -o share.o
* ld -shared share.o -o share.sl
*
* kishan@hackorama.com ( www.hackorama.com ) Feb 2001
*
*/

#include

void
share( int value )
{
fprintf( stdout, "\nthe answer is %d\n", value );
}

------------ cut ----------

#cc -fPIC -c share.c -o share.o
#ld -shared share.o -o share.sl


main.c [ using dlopen(), dlsym() ]

coding ---

/*
* 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
#include

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);
}

----------------------- cut _------------------

cc main.c -o main -ldl


On HP-UX :

share.c

coding ---

/*
* sample code for creating a shared library on HP-UX
*
* cc +z -c share.c -o share.o
* ld -b share.o -o share.sl
*
* kishan@hackorama.com ( www.hackorama.com ) Feb 2001
*
*/

#include

void
share( int value )
{
fprintf( stdout, "\n\nthe answer is %d\n\n", value );
}


------------------------- cut -------------------------

cc +z -c share.c -o share.o
ld -b share.o -o share.sl
main.c [ using shl_load(), shl_findsym() ]

coding ---

/*
* sample code for loading , calling a method from
* and unloading a shared library on HP-UX
*
* cc main.c -o main
*
* If using a 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
#include

int
main( void )
{
const char* sh_lib = "share.sl";
const char* method = "share";

void (*fp) ( int value ) = NULL ;
shl_t handle = shl_load( sh_lib , BIND_IMMEDIATE , NULL );

if( handle == NULL ){
fprintf( stderr, "\nfailed loading %s\n", sh_lib);
exit(1);
}else{
shl_findsym( &handle, method, TYPE_UNDEFINED, &fp );
if( fp == NULL )
fprintf( stderr, "\nfailed getting method %s\n", method);
else
fp( 42 );

if ( shl_unload( handle ) != 0 )
fprintf( stderr, "\nfailed unloading %s\n", sh_lib);
}

exit(0);
}

------------------------- cut ----------------------

cc main.c -o main


The linux sample code should work for other UNIX flavours, which use dlopen()/dlsym(). Only HP-UX uses shl_load()/shl_findsym(). Please look at the man pages of cc and ld for the correct flags to be used for the particular version of UNIX.

No comments: