This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: [Q] automagic define/undefine with dynamic-link/dynamic-unlink


Marius Vollmer writes:
 > Ole Myren R|hne <mrohne@mixing.uio.no> writes:
 > >
 > > I want to use the _init and _fini symbols to automatically
 > > define/undefine extensions as a dynamic library is linked/unlinked. 
 > 
 > Hmm, off hand I would say that this doesn't work.  But, obviously, it
 > does.  Strange.
 > 
 > I would have said that using the .init/.fini sections of the ELF
 > object format is not that simple. 

dlopen looks for the_init/_finit symbols, not the .init/.fini
sections. I had to use `ld -shared' instead of `gcc -shared'. gcc
tries to link in /usr/lib/crti.o, which gives a clash.

 > You would have to use global C++ objects, or some gcc extension.

You are right, but let's avoid C++ ;-)

Using the gcc extension you mention, mysin.c look like:

#include <math.h>
#include <guile/gh.h>
SCM mysin(SCM x) {return gh_double2scm(sin(gh_scm2double(x)));}
void __attribute__ ((constructor)) mysin_init(void) {gh_new_procedure("mysin", mysin, 1, 0, 0);}
void __attribute__ ((destructor))  myxin_fini(void) {gh_eval_str("(undefine mysin)");}

$ gcc -c -fPIC mysin.c
$ gcc -shared -o mysin.so mysin.o

One could even hope that this would work on all platforms that have
gcc and support dynamic linking.

Ole