[comp.sys.sun] Sun-Spots Digest, v10n86 Miscellaneous

basile@soleil.cea.fr (Basile STARYNKEVITCH) (06/05/91)

About dynamic loading, I suggest using the dlopen facilities (describes
in SunOs4.1 man 3 dlopen). I am using them and am very satisfied...
Performances are good (less than 70 microseconds on a Sun 4/60
SparcStation1 to find a newly loaded adress in a 20000 symbols
dynamically loaded library, given its symbol - with the dlsym function)
You should just build a shared library (compiling it with -pic or -PIC option, and ld-ing it with -assert pure-text:
suppose func1 in f1.c, func2 in f2.c
	cc -pic -c f1.c -o f1.o
	cc -pic -c f2.c -o f2.o
	ld -assert pure-text f1.o f2.o -o sharedlib.so)

Then in your program - ommitting test stuff:
	#include <dlfcn.h>

	void *libhandle;
	void (*myloadedfunc1) ();
	void (*myloadedfunc2) ();
	libhandle = dlopen("sharedlib.so", 1);
	/* if (!libhandle) error... */
	myloadedfunc1 = dlsym(libhandle, "_func1");
	/* if (!myloadedfunc1) error... */
	myloadedfunc2 = dlsym(libhandle, "_func2");
	/* if (!myloadedfunc2) error... */
	/* call to func1(1):: */
	(*myloadedfunc1) (1);

Caveat:: the dynamically loaded functions - here func1 and func2 should call external functions or variables already in your program: either in  an initially used shared library such as libc.*.so, or in your *.o, or already referenced in a statically linked library. For instance, if func1 calls pprintf in static lib: libcps.a, you should reference pprintf in you bare program - for instance in a static routine or array). Of course, two different dynamically loaded libraries cannot reference to each other wi




thout using global functions pointers such as myloadedfunc1...

Basile STARYNKEVITCH
Commissariat a l'Energie Atomique
DMT/SERMA
CE. Saclay bat470
91191 GIF/YVETTE CEDEX
France