linux创建动态库

  

[1]新建源程序sharelib.c

 /*************************************************************************
> File Name: sharelib.c
> Author: copener
> Mail: hanmingye@foxmail.com
> Created Time: 2015年05月14日 星期四 09时03分18秒
************************************************************************/ #include<stdio.h>
/*
*插入法排序函数
*输入参数:*array是将要排序的数组,length是元素的长度
* */
void insert_sort(int *array, int length){
int i,j;
for(i=; i<length; i++){
int temp;
j=i;
temp = array[j]; while(j>){
if(temp < array[j-]){
array[j]=array[j-];
j--;
}else{
break;
} array[j] = temp;
}
}
return;
} /*
*二分法查找函数
*输入参数:*array是将要查找的数组,item是要查找的元素,length是元素的长度
* */
int binary_search(int *array, int item, int length){
int high, low, mid;
high = length;
low = ;
mid = (high + low)/; while(low < high){
printf("high:%d low:%d mid:%d\r\n",high,low,mid);
if(array[mid] > item){
if(low==(high-) || high == mid ){
return -;
}
printf("array[%d]>%d\r\n",mid,item);
high = mid;
mid = (high + low)/; }else if(array[mid]<item){
if(low==mid || high == (low+)){
return -;
}
printf("array[%d]<%d\r\n",mid,item);
low = mid;
mid = (high + low)/; }else{
return mid;
}
}
return -;
}

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

 oee@copener:~/workspace/test/sharelib$ gcc -shared -fPIC -o sharelib.so sharelib.c
oee@copener:~/workspace/test/sharelib$ ls
sharelib.c sharelib.so
 //关于gcc 中的-shared 参数
-shared
Produce a shared object which can then be linked with other objects to form an executable. Not all systems support this option. For
predictable results, you must also specify the same set of options used for compilation (-fpic, -fPIC, or model suboptions) when you
specify this linker option.[] //-shared会与-fPIC参数一起使用来生成动态库文件

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

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

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

 /*************************************************************************
> File Name: testapp.c
> Author: copener
> Mail: hanmingye@foxmail.com
> Created Time: 2015年05月14日 星期四 11时25分46秒
************************************************************************/ #include<stdio.h>
#include "sharelib.h" int main(void){
int item;
int pos;
int i;
int array[] ={,,,,}; //排序
insert_sort(array, );
for(i=; i<; i++){
printf("%d ",array[i]);
} //二分法查找
printf("\r\nplease input a number:\r\n");
scanf("%d", &item); pos = binary_search(array, item, );
if(pos == -){
printf("can't find or data not sort!\r\n");
}
else{
printf("done! the position is %d\r\n",(pos+));
} return ;
}

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

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

[6]测试运行testapp

 oee@copener:~/workspace/test/sharelib$ ./testapp 

 please input a number:

 input end, processing...
high: low: mid:
array[]>
high: low: mid:
done! the position is

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

上一篇:STM32C8T6 JTAG使用到PB3|PB4|PA13|PA14|PB15端口做普通IO时,需禁止JTAG!


下一篇:derby的三大缺陷