Chinaunix首页 | 论坛 | 博客
  • 博客访问: 712101
  • 博文数量: 134
  • 博客积分: 3207
  • 博客等级: 中校
  • 技术积分: 1995
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-01 20:47
文章分类

全部博文(134)

文章存档

2022年(1)

2020年(7)

2018年(2)

2016年(5)

2015年(14)

2014年(21)

2013年(3)

2012年(1)

2011年(15)

2010年(30)

2009年(35)

分类: LINUX

2014-03-14 15:47:47

C代码
  1. #include    
  2. #include    
  3.   
  4. int main(void){   
  5.    int (*myadd)(int a,int b);//fuction pointer   
  6.    void *handle;   
  7.       
  8.    handle=dlopen("./libmyadd.so",RTLD_LAZY);//open lib file   
  9.    myadd=dlsym(handle,"output");//call dlsym function   
  10.       
  11.   
  12.    int result=myadd(1,2);   
  13.    dlclose(handle);   
  14.    printf("%d\n",result);     
  15. }  

 以上为调用程序test8.c,

以下为库程序test7.c

C代码
  1. int output(int a,int b){   
  2.    int x=a+b;   
  3.    return x;   
  4. }  

 gcc -shared -o libmyadd.so test7.c
gcc -ldl -o test8 test8.c
./test8
3

 

 

不管什么库文件,你都既要在包含.h文件(不然编译通不过:有未声明的函数),也要在gcc选项里面指定.so文件的位置(不然链接通不过:未知的符号)  比如  gcc -I include_path -L lib_path -lyourlib  include_path改成你头文件的目录  lib_path改成你动态库文件的目录  -lyourlib 改成l加上你要引用的库文件名字  比如libpthread.so就改成-lpthread 
阅读(4574) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~