码迷,mamicode.com
首页 > 系统相关 > 详细

linux创建动态库

时间:2015-05-14 18:00:19      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

  

[1]新建源程序sharelib.c

 1 /*************************************************************************
 2     > File Name: sharelib.c
 3     > Author: copener
 4     > Mail: hanmingye@foxmail.com
 5     > Created Time: 2015年05月14日 星期四 09时03分18秒
 6  ************************************************************************/
 7 
 8 #include<stdio.h>
 9 /*
10 *插入法排序函数
11 *输入参数:*array是将要排序的数组,length是元素的长度
12 * */
13 void insert_sort(int *array, int length){
14     int i,j;
15     for(i=1; i<length; i++){
16         int temp;
17         j=i;
18         temp = array[j];
19 
20         while(j>0){
21             if(temp < array[j-1]){
22                 array[j]=array[j-1];
23                 j--;
24             }else{
25                 break;
26             }
27 
28             array[j] = temp;
29         }
30     }
31     return;
32 }
33 
34 /*
35 *二分法查找函数
36 *输入参数:*array是将要查找的数组,item是要查找的元素,length是元素的长度
37 * */
38 int binary_search(int *array, int item, int length){
39     int high, low, mid;
40     high = length;
41     low  = 0;
42     mid  = (high + low)/2;
43     
44     while(low < high){
45         printf("high:%d low:%d mid:%d\r\n",high,low,mid);
46         if(array[mid] > item){
47             if(low==(high-1) || high == mid ){
48                 return -1;
49             }
50             printf("array[%d]>%d\r\n",mid,item);
51             high = mid;
52             mid  = (high + low)/2;
53 
54         }else if(array[mid]<item){
55             if(low==mid || high == (low+1)){
56                 return -1;
57             }
58             printf("array[%d]<%d\r\n",mid,item);
59             low = mid;
60             mid  = (high + low)/2;
61 
62         }else{
63             return mid;
64         }
65     }
66     return -1;
67 }

 

[2]生成sharelib.so的动态库文件

1 oee@copener:~/workspace/test/sharelib$ gcc -shared -fPIC -o sharelib.so sharelib.c
2 oee@copener:~/workspace/test/sharelib$ ls
3 sharelib.c  sharelib.so 

 

1 //关于gcc 中的-shared 参数
2  -shared
3            Produce a shared object which can then be linked with other objects to form an executable.  Not all systems support this option.  For
4            predictable results, you must also specify the same set of options used for compilation (-fpic, -fPIC, or model suboptions) when you
5            specify this linker option.[1]
6 
7 
8 //-shared会与-fPIC参数一起使用来生成动态库文件

 

 

[3]添加sharelib.h的头文件引用

 1 /*************************************************************************
 2     > File Name: sharelib.h
 3     > Author: copener
 4     > Mail: hanmingye@foxmail.com
 5     > Created Time: 2015年05月14日 星期四 11时23分18秒
 6  ************************************************************************/
 7 
 8 extern void insert_sort(int *array, int length);
 9 extern int binary_search(int *array, int item, int length);
10 extern void bubble_sort(int * array, int length);

 

 

[4] 新建程序testapp.c调用动态库函数测试

 1 /*************************************************************************
 2     > File Name: testapp.c
 3     > Author: copener
 4     > Mail: hanmingye@foxmail.com
 5     > Created Time: 2015年05月14日 星期四 11时25分46秒
 6  ************************************************************************/
 7 
 8 #include<stdio.h>
 9 #include "sharelib.h"
10 
11 int main(void){
12     int item;
13     int pos;
14     int i;
15     int array[5] ={5,9,2,3,1};    
16 
17     //排序
18     insert_sort(array, 5);
19     for(i=0; i<5; i++){
20         printf("%d ",array[i]);
21     }
22 
23     //二分法查找
24     printf("\r\nplease input a number:\r\n");
25     scanf("%d", &item);
26 
27     pos = binary_search(array, item, 5);
28     if(pos == -1){
29         printf("can‘t find or data not sort!\r\n");
30     }
31     else{
32         printf("done! the position is %d\r\n",(pos+1));
33     }
34 
35     return 0;
36 }

 

 

[5]编译测试源码链接动态库sharelib.so,生成testapp可执行程序 

1 oee@copener:~/workspace/test/sharelib$ gcc -o testapp testapp.c ./sharelib.so 
2 oee@copener:~/workspace/test/sharelib$ ls
3 sharelib.c  sharelib.h  sharelib.so  testapp  testapp.c

 

 

[6]测试运行testapp

1 oee@copener:~/workspace/test/sharelib$ ./testapp 
2 1 2 3 5 9 
3 please input a number:
4 2
5 input end, processing...
6 high:5 low:0 mid:2
7 array[2]>2
8 high:2 low:0 mid:1
9 done! the position is 2

 

 

[7]小结:库写好后,只要提供sharelib.so库和sharelib.h函数头文件给使用者即可。链接库在编译程序时要放在被编译的源码文件之后,否则可能会链接失败。

 

linux创建动态库

标签:

原文地址:http://www.cnblogs.com/COpener/p/4503774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!