Mail Archives: cygwin/2003/03/19/07:11:17
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
> <sys/modctl.h> (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 <stdio.h>
> 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 <stdio.h>
> #include <stdlib.h>
> #include <dlfcn.h>
> 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/
- Raw text -