-----------------------------------------------------------------------------dlsym-----------------------------------------------------------------------------函數
測試dlsym打開的函數指針能不能屢次調用測試
#include <stdio.h> #include <dlfcn.h> #include <unistd.h> #include <time.h> int main(){ int (*func)(int a, int b); void *handle = NULL; int c; char *myso = "./mylib.so"; if((handle = dlopen(myso, RTLD_NOW)) != NULL) { printf("success\n"); func = (void(*)())dlsym(handle, "add"); if(func != NULL){c=(*func)(1,2);} printf("1--%d",c); if(func != NULL){c=(*func)(2,2);} printf("\n2--%d",c); dlclose(handle); } else printf("dlopen - %s\n", dlerror()); }
gcc main.c -ldl -rdynamicspa
#include <stdlib.h> #include <stdio.h> int add(int a,int b){ return a+b; }
gcc -fPIC -shared -o mylib.so add.c指針
調用:code
./a.outblog
執行結果:io