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函数头文件给使用者即可。链接库在编译程序时要放在被编译的源码文件之后,否则可能会链接失败。
linux创建动态库的更多相关文章
- Linux Qt动态库的创建和使用
一.创建动态库 编写一个共享库类,比如: //..base.h class Base : public QObject { Q_OBJECT public: ); void PrintLog(QStr ...
- 深入理解LINUX下动态库链接器/加载器ld-linux.so.2
[ld-linux-x86-64.so.2] 最近在Linux 环境下开发,搞了好几天 Compiler 和 linker,觉得有必要来写一篇关于Linux环境下 ld.so的文章了,google上搜 ...
- 谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH PKG_CONFIG_PATH
谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH PKG_CONFIG_PATH 转载自:http://blog.chinaunix.net/xmlrpc.ph ...
- Linux下动态库生成和使用
Linux下动态库生成和使用 一.动态库的基本概念 1.动态链接库是程序运行时加载的库,当动态链接库正确安装后,所有的程序都可以使用动态库来运行程序.动态链接库是目标文件的集合,目标文件在动态链接库中 ...
- Linux生成动态库系统
Linux生成动态库系统 一个.说明 Linux下动态库文件的扩展名为 ".so"(Shared Object). 依照约定,全部动态库文件名称的形式是libname.so(可能在 ...
- Linux下动态库和静态库的生成和使用
1.准备头文件和源文件 hello.h #ifndef HELLO_H #define HELLO_H void hello(const char *name): #endif hello.c #in ...
- DELPHI开发LINUX的动态库
DELPHI开发LINUX的动态库 WINDOWS的动态库是.dll,这个大家都知道. LINUX也有动态库,扩展名是.so,现在DELPHI也能开发LINUX的动态库哦. DELPHI对LINUX的 ...
- C++创建动态库
[C++]创建动态库 有很多方法,这个只是其中一种 比较简洁的方法. char* __stdcall correction(char* str) char *_result = new char[se ...
- Linux 创建静态库(.a)和动态库(.so)
0. 回顾一下 gcc 选项 ============================================== -E : 仅做预处理,例如去注释,宏展开,include 展开等 -S : ...
随机推荐
- React学习笔记
1.React的一开始出发点是:用于开发数据不断变化的大型应用程序(Building large applications with data that changes over time) 2.生命 ...
- java运算符
赋值运算符 int num1=10; int num2=30; System.out.println(num1+num2); 算术运算符 int num=20; System.out.println( ...
- oracle_空值判断
Oracle空值测试 比较项目 is null is not null nvl 说明 NULL true false true null true false true 'NULL' fals ...
- logsatsh input 插件之 collectd
logsatsh input 插件之 collectd 标签(空格分隔): logstash 作用:用于监控内存,cpu,磁盘I等信息 未完待续,时间未定. 参考: logstash 官网 elast ...
- 【EXCEL】冻结窗口的设置
在excel使用时,表格内容很多时,为查看方便,需要冻结一部分内容,冻结窗口分为以下几种情况,分别进行解析. 工具/原料 excel 冻结首行 1 打开需要的表格,点击“视图”----冻结窗格,点击“ ...
- 学习Excel 十大函数
云课堂视频教程 笔记总结: URL:http://study.163.com/course/courseLearn.htm?courseId=1009026#/learn/video?lessonId ...
- LeetCode 7 -- String to Integer (atoi)
Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...
- mybatis配置文件查询参数的传递
通常来说,参数传递可以使用#与$进行编写,但是使用#的效率更高,使用$方式,查看日志更方便些,尤其是当执行的sql语句非常麻烦的时候. 1) 接口 形式 以下方式 [传递参数是一个实体] public ...
- yii2.0邮箱发送
邮件发送配置: 打开配置文件将下面代码添加到 components => [...]中(例:高级版默认配置在/common/config/main-local.php) 'mai ...
- 我是一只IT小小鸟----读书笔记
人生如同打牌,能抓到一手好牌固然是运气好,但重要的是,你不能靠运气生活,而要琢磨怎样把手上这把看上去“不怎么样的”牌打好. 要了解一种主流的软件开发与运行平台,熟练掌握一种开发工具,这样就具备了进行开 ...