[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函数头文件给使用者即可。链接库在编译程序时要放在被编译的源码文件之后,否则可能会链接失败。

linux创建动态库的更多相关文章

  1. Linux Qt动态库的创建和使用

    一.创建动态库 编写一个共享库类,比如: //..base.h class Base : public QObject { Q_OBJECT public: ); void PrintLog(QStr ...

  2. 深入理解LINUX下动态库链接器/加载器ld-linux.so.2

    [ld-linux-x86-64.so.2] 最近在Linux 环境下开发,搞了好几天 Compiler 和 linker,觉得有必要来写一篇关于Linux环境下 ld.so的文章了,google上搜 ...

  3. 谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH PKG_CONFIG_PATH

    谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH  PKG_CONFIG_PATH 转载自:http://blog.chinaunix.net/xmlrpc.ph ...

  4. Linux下动态库生成和使用

    Linux下动态库生成和使用 一.动态库的基本概念 1.动态链接库是程序运行时加载的库,当动态链接库正确安装后,所有的程序都可以使用动态库来运行程序.动态链接库是目标文件的集合,目标文件在动态链接库中 ...

  5. Linux生成动态库系统

    Linux生成动态库系统 一个.说明 Linux下动态库文件的扩展名为 ".so"(Shared Object). 依照约定,全部动态库文件名称的形式是libname.so(可能在 ...

  6. Linux下动态库和静态库的生成和使用

    1.准备头文件和源文件 hello.h #ifndef HELLO_H #define HELLO_H void hello(const char *name): #endif hello.c #in ...

  7. DELPHI开发LINUX的动态库

    DELPHI开发LINUX的动态库 WINDOWS的动态库是.dll,这个大家都知道. LINUX也有动态库,扩展名是.so,现在DELPHI也能开发LINUX的动态库哦. DELPHI对LINUX的 ...

  8. C++创建动态库

    [C++]创建动态库 有很多方法,这个只是其中一种 比较简洁的方法. char* __stdcall correction(char* str) char *_result = new char[se ...

  9. Linux 创建静态库(.a)和动态库(.so)

    0. 回顾一下 gcc 选项 ============================================== -E : 仅做预处理,例如去注释,宏展开,include 展开等 -S : ...

随机推荐

  1. Android 6.0 新特性

    首先谈一谈Android 6.0的一些新特性 锁屏下语音搜索 指纹识别 更完整的应用权限管理 Doze电量管理 Now onTap App link 在开发过程中与我们关系最密切的就是"更完 ...

  2. MYSQL 处理批量更新数据的一些经验。

    首先,我们需要了解下MYSQL CASE EXPRESSION 语法. 手册传送门:http://dev.mysql.com/doc/refman/5.7/en/control-flow-functi ...

  3. hosts持续更新

    Google hosts网址: https://laod.cn/hosts/2016-google-hosts.html

  4. Number of 1 Bits(Difficulty: Easy)

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  5. SQL server存储过程语法及实例(转)

    存储过程如同一门程序设计语言,同样包含了数据类型.流程控制.输入和输出和它自己的函数库. --------------------基本语法-------------------- 一.创建存储过程cr ...

  6. 大视野3562 [SHOI2014]神奇化合物

    http://www.lydsy.com/JudgeOnline/problem.php?id=3562 //Accepted 6020 kb 1012 ms //由于题目的特殊要求:然而,令科学家们 ...

  7. Permutation

    (M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II

  8. 六大免费网站数据采集器对比(火车头,海纳,云采集,ET,三人行,狂人采集)

    2013年02月27日 PHP开源系统 暂无评论 阅读 497 views 次 在目前的站长圈内,比较流行的采集工具有很多,但是总结起来,比较出名的免费的就这么几个:火车头,海纳,云采集,ET,三人行 ...

  9. 转载——C++控制台贪吃蛇代码

    游戏截图: 以下是3个代码文件: Snake_Class.h文件: 1 #ifndef SNAKE 2 #define SNAKE 3 4 #include<windows.h> 5 #i ...

  10. js单击自动选择文本

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...