[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
[an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]![]() |
![]() |
![]() |
|||||||||||||||||||||||||
|
|||||||||||||||||||||||||||
![]() |
![]() |
![]() |
Kasper Hauge wrote: > Som jeg har forstået det, vil denne stump program blive udført af den > første der kalder DLL'en. Jeg rodede fornyeligt med det, her er et eksempel jeg lavede: Program der dynamisk loader en .so fil og kalder funktioner i den. For simplificet antager jeg at path to .so filen er fast. Når dlopen loader .so filen udføres _init, og når den unloades så udføres _fini funktionerne. Bemærk det kræver -nostartfiles flaget til kompileren. libload.c: #include <stdio.h> #include <dlfcn.h> #include <unistd.h> // path til .so fil. const char fname[]="/home/zqex/code/c/sharedobject/libtest.so.0.0"; int main() { char* err; void* handle = dlopen(fname,RTLD_NOW); // RTLD_NOW resolve dependencies now. void (*function1)(void); void (*function2)(double); if (handle == NULL) { printf("Opening library error: %s",dlerror()); exit(1); } dlerror(); // Flush errorstate function1=dlsym(handle,"test1"); if ((err=dlerror()) != NULL) { printf("%s\n",err); exit(1); } dlerror(); // Flush errorstate function2=dlsym(handle,"test2"); if ((err=dlerror()) != NULL) { printf("%s\n",err); exit(1); } function1(); function2(2.0); dlclose(handle); return 0; } Kode der kompileres til et shared object (.so) fil (testlib.c) #include <stdio.h> void test1(void) { printf("Hello World\n"); } void test2(double x) { printf("%f\n",x*x); } // Debug at init and finialization void _init(void) { printf("Initializing SO\n"); } void _fini(void) { printf("Deinitializing SO\n"); } Bemærk den skal kompiles med specielle switches Her er en make fil der opretter so filen, og programmer der dynamisk loader den, og kalder funktioner i so filen. makefile: all : testlib.so libload clean : rm *.o ; rm *.so.0.0 ; rm core ; rm libload #compiling library function files: testlib.o : testlib.c gcc -fPIC -Wall -ggdb -c testlib.c # -fPIC relocatable code, -Wall all warnings, -ggdb debugging testlib.so : testlib.o gcc -ggdb -shared -nostartfiles -W1,-soname,libtest.so.0 \ -o libtest.so.0.0 testlib.o -lc # -nostartfiles allows _init,_fini. #compiles test program: libload : libload.c gcc -rdynamic -ggdb -Wall -o libload libload.c -ldl -- Mvh. Carsten Svaneborg
![]() |
![]() |
![]() |
||||||||||||
|
||||||||||||||
![]() | ||||||||||||||
|
||||||||||||||
![]() |
![]() |
![]() |