首页 > 其他 > 详细

C - dlopen dlsym

时间:2019-08-21 21:02:05      阅读:107      评论:0      收藏:0      [点我收藏+]

 

 

-----------------------------------------------------------------------------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 -rdynamic

 

#include <stdlib.h>
#include <stdio.h>


int add(int a,int b){
 return a+b;
}

gcc -fPIC -shared -o mylib.so add.c

 

调用:

技术分享图片

./a.out

执行结果:

技术分享图片

 

C - dlopen dlsym

原文:https://www.cnblogs.com/wangqiwen-jer/p/11390828.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!