004
25.04.2003, 10:42 Uhr
virtual
Sexiest Bit alive (Operator)
|
Man muß sich offenbar ein wenig auf die Hinterbeine Stellen. So hat es vom Prinzip by mir geklappt:
Das hier ist meine Share Lib:
C++: |
/* myso.c */ #include <stdio.h>
void func() { FILE* log = fopen("myso.log", "a"); if (NULL != log) fprintf(log, "func called\n"); fclose(log); }
void [b]my_init[/b]() { FILE* log = fopen("myso.log", "a"); if (NULL != log) fprintf(log, "_init called\n"); fclose(log); }
void [b]my_fini[/b]() { FILE* log = fopen("myso.log", "a"); if (NULL != log) fprintf(log, "_fini called\n"); fclose(log); }
|
Nun muß ich dem Linker nur noch mitteilen, daß my_init und my_fini die Routinen sind, die die Rolle von _init und _fini übernehmen sollen:
C++: |
# makefile libmyso.so : myso.o gcc -fpic -shared [b]-Xlinker -init -Xlinker my_init -Xlinker -fini -Xlinker my_fini[/b] -o libmyso.so myso.o
myso.o : myso.c gcc -fpic -shared -c -o myso.o myso.c
|
P.S.: Nachgeprüft mit dieem Programm:
C++: |
#include <stdio.h> #include <stdlib.h> #include <dlfcn.h>
int main( int argc, char** argv) { const char* prgname; const char* libname; const char* symbolname; void* libhandle; void* symbolhandle; int mode = RTLD_LAZY; int ret; /* * Determine invokation name */ prgname = argv[0] + strlen(argv[0]); while (prgname>argv[0] && prgname[-1]!='/') prgname--;
/* * Check arguments */ argv++; argc--; while (argc) { if (strcmp(*argv, "--")==0) { argv++; argc--; break; }else if (**argv != '-') { break; }else { if (strcmp(*argv, "-l") == 0 || strcmp(*argv, "--lazy") == 0) { mode = RTLD_LAZY; argv++; argc--; }else if (strcmp(*argv, "-n") == 0 || strcmp(*argv, "--now") == 0) { mode = RTLD_NOW; argv++; argc--; }else { fprintf(stderr, "*** ERROR: %s: unknown option %s\n" "usage: %s [-l|-n] library symbol\n", prgname, *argv, prgname); exit(EXIT_FAILURE); } } } if (argc != 2) { fprintf(stderr, "*** ERROR: %s: missing arguments\n" "usage: %s [-l|-n] library symbol\n", prgname, prgname); exit(EXIT_FAILURE); } libname = argv[0]; symbolname = argv[1];
/* * Open the library */ libhandle = dlopen(libname, mode); printf("dlopen(%s, %s) = %p\n", libname, mode==RTLD_LAZY? "RTLD_LAZY":"RTLD_NOW", libhandle); if (libhandle == NULL) { fprintf(stderr, "*** ERROR: %s: dlopen failed: %s\n", prgname, dlerror()); exit(EXIT_FAILURE); }
/* * Get the symbol */ symbolhandle = dlsym(libhandle, symbolname); printf("dlsym(%p, %s) = %p\n", libhandle, symbolname, symbolhandle); if (symbolhandle == NULL) { fprintf(stderr, "*** ERROR: %s: dlsym failed: %s\n", prgname, dlerror()); dlclose(libhandle); exit(EXIT_FAILURE); }
/* * close the library */ ret = dlclose(libhandle); printf("dlclose(%p) = %d\n", libhandle, ret); if (ret != 0) { fprintf(stderr, "*** ERROR: %s: dlclose failed: %s\n", prgname, dlerror()); exit(EXIT_FAILURE); }
exit(EXIT_SUCCESS); }
|
-- Gruß, virtual Quote of the Month Ich eß' nur was ein Gesicht hat (Creme 21) Dieser Post wurde am 25.04.2003 um 10:44 Uhr von virtual editiert. |