Chinaunix首页 | 论坛 | 博客
  • 博客访问: 375904
  • 博文数量: 124
  • 博客积分: 2911
  • 博客等级: 少校
  • 技术积分: 1050
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-15 15:57
文章分类

全部博文(124)

文章存档

2012年(6)

2011年(26)

2010年(92)

我的朋友

分类: LINUX

2010-07-07 20:45:58

1.linux下使用gcc 生成共享文件.so的方法.
****************************************************************************************************************
    生成步骤大致如下:
    1:编写源文件:                hello1_so.h=>hello1_so.c、hello2_so.c、hello3_so.c、hello4_so.c
    2:使用gcc命令生成.so文件:    #gcc hello1_so.c hello2_so.c hello3_so.c hello4_so.c  -fPIC -shared -o hello_so
    3:test_hello_so.c使用.so文件:    编译: $ gcc test_hello_so.c -L. -lhello_so -o test_hello_so
****************************************************************************************************************
-shared 该选项指定生成动态连接库(让连接器生成T类型的导出符号表,有时候也生成弱连接W类型的导出符号),不用该标志外部程序无法连接。相当于一个可执行文件
-fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。
****************************************************************************************************************
-L.:表示要连接的库在当前目录中
-lhello_so:-l表示库文件,默认是前面加入lib,后面加上.so来确定库文件名字的.
****************************************************************************************************************
2.dlopen 和scandir的使用.
****************************************************************************************************************
For Instance :

strcpy(auth_symbols[0].name, "hb_auth_calc");//.so文件中的三个函数
strcpy(auth_symbols[1].name, "hb_auth_atype");
strcpy(auth_symbols[2].name, "hb_auth_nkey");

n = scandir(AUTH_MODULE_DIR, &namelist, SCANSEL_C &so_select, 0);//返回文件个数

char* obj_path;//.so文件所在路径
obj_path = malloc((strlen(AUTH_MODULE_DIR)+strlen(namelist[a]->d_name)+2)*sizeof(char));
sprintf(obj_path,"%s/%s", AUTH_MODULE_DIR, namelist[a]->d_name);

auth = MALLOCT(struct auth_type);
auth_symbols[0].function = (void **)&auth->auth;
auth_symbols[1].function = (void **)&auth->atype;
auth_symbols[2].function = (void **)&auth->needskey;

if ((auth->dlhandler = dlopen(obj_path, RTLD_NOW)) == NULL) {***}


阅读(805) | 评论(0) | 转发(0) |
0

上一篇:Java学习之路

下一篇:MD5_C_Linux

给主人留下些什么吧!~~