This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

_init() and _fini() for dynamically loaded libraries?


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/


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