Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com X-Authentication-Warning: localhost.localdomain: ronald owned process doing -bs Date: Wed, 19 Mar 2003 13:22:42 +0100 (CET) From: Ronald Landheer-Cieslak X-X-Sender: ronald AT localhost DOT localdomain To: Daniel Daboul cc: cygwin AT cygwin DOT com Subject: Re: _init() and _fini() for dynamically loaded libraries? In-Reply-To: <20030319112418.GO24792@legend.msil.sps.mot.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII gcc supports a couple of special attributes for init and fini - type functions: __attribute__((constructor)) and __attribute__((destructor)) If you add void _init() __attribute__((constructor)); void _fini() __attribute__((destructor)); somewhere to the top of mylib.c, your example will work as expected. HTH rlc On Wed, 19 Mar 2003, Daniel Daboul wrote: > Does Cygwin support initialization and cleanup for libraries loaded > dynamically via dlopen() in a way similar to Linux and Solaris? If > yes, how can I get it to work? I attached a simple test case below, > with comments telling how to compile/link. The output under cygwin > is: > > calculating > doubled in library: 2.468000 > > So it basically works, but neither _init() nor _fini() get > executed. Under Solaris the output is: > > initializing > calculating > doubled in library: 2.468000 > finishing > > (There I link with "ld -Bshareable mylib.o -o libmylib.dll" and "gcc > -Wall -o caller caller.c -ldl".) Solaris declares _init and _fini in > (actually returning int), which does not exists in my > cygwin installation, but I'm not sure this is enough to conclude that > it's unsupported. I didn't find help in the documentation or > mail-archives. -- Daniel > > /* mylib.c > gcc -Wall -c mylib.c > gcc -shared -Wl,--export-all mylib.o -o libmylib.dll > */ > #include > double dub(double in1) {printf("calculating\n");return 2*in1;} > void _init() {printf("initializing\n");} > void _fini() {printf("finishing\n");} > > /* caller.c > gcc -Wall -o caller caller.c > ./caller > */ > #include > #include > #include > int main() { > void *handle; > double (*pdub)(double); > char *error; > handle = dlopen("./libmylib.dll",RTLD_LAZY); > if(!handle) {fprintf(stderr,"%s\n",dlerror()); exit(1);} > pdub =dlsym(handle,"dub"); > if((error = dlerror())!=NULL) {fprintf(stderr,"%s\n",error); exit(1);} > printf("doubled in library: %f\n",(*pdub)(1.234)); > dlclose(handle); > return 0; > } > > -- > Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple > Bug reporting: http://cygwin.com/bugs.html > Documentation: http://cygwin.com/docs.html > FAQ: http://cygwin.com/faq/ > -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Bug reporting: http://cygwin.com/bugs.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/