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 : ...
随机推荐
- linux 两个文件合并
可以使用cat命令,有两种实现的方式,一种将两个文件合并的到一个新的文件,另一种将一个文件追加到另一个文件的末尾. 方法一:使用cat命令从文件中读入两个文件,然后将重定向到一个新的文件.这种方法可以 ...
- css cursor 的可选值(鼠标的各种样式)
crosshair; 十字准心 The cursor render as a crosshair游标表现为十字准线 cursor: pointer; cursor: hand;写两个是为了照顾IE5, ...
- 错误:媒体集有 2 个媒体簇,但只提供了 1 个 sql2005 备份错误。
错误:媒体集有 2 个媒体簇,但只提供了 1 个 sql2005 2010-10-19 11:44:06| 分类: sql|举报|字号 订阅 ql2005备份的时候,选择备份路径是一方面,但 ...
- 项目组J2ee程序员的标志,你中招没 转载+评论
原文在此 校园级别的程序员的标志: 代码中最多的是嵌套if(null == xxx),还要告诉你,null必须写在前面,我靠. 防止把==写成=,c语言时代常犯的错误.由于null不能做左值,在写=的 ...
- 《2---关于JDBC编程过程中驱动配置问题》
说明:我在Editplus中编写了一个简单的JDBC程序,用来测试是否和数据库连接正确.读者如有其它疑问,可以留言交流. [1]程序如下: import java.sql.*; public clas ...
- “用户、组或角色'XXX'在当前数据库中已存在”问题
一般在还原数据库后,给这个数据库添加一个登录名时出现. 例如数据库备份文件中已经包含了用户abc,现在还原了数据库,然后发现现有数据库中没有abc这个用户,想要新建一个abc用户,作为该数据库的own ...
- CMD和AMD区别的概括
CMD和AMD区别 AMD CMD 关于依赖的模块 提前执行(不过 RequireJS 从 2.0 开始,也改成可以延迟执行(根据写法不同,处理方式不同)), 延迟执行 关于依赖的位置 依赖前置 ...
- Android计算器尝试
学了一段时间Android了,一直都是在看,没有什么尝试,刚好最近大致学会了gridview配合simpleadpter的使用,于是想着动手练习一下,就选择了写一个最简单的计算器来实现. 只包含+-* ...
- Vue.js的计算属性
开始用vue会把所有的模版上的数据都放到data属性里,或者有的时候data属性里变量多了之后觉得有些只是用一次的变量就直接写到模版里了,后来看到同组的同事在用computed属性,就又去查了一下ap ...
- C++虚方法(虚函数)随笔
本文不讨论虚函数的原理,只简单总结下虚函数的常用事项. 虚函数(虚方法)是C++动态联编 实现多态的重要手段,在函数声明时使用关键字virtual即可,如: virtual void func(voi ...