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 : ...
随机推荐
- Dom学习笔记
今天老师出了一道面试题目:取到表单里面的textbox的值,两种方法.知道一种,老师说的什么dom,我竟然不知道. 以前学html的时候,老师也重来没有提到dom的概念.javaScript只是学了一 ...
- PL/SQL Developer记住用户名密码
在使用PL/SQL Developer时,为了工作方便希望PL/SQL Developer记住登录Oracle的用户名和密码: 设置方法:PL/SQL Developer ->tools-> ...
- 将命令添加到shell脚本中然后设置开机自启动
例如开机自启动nginx 编写一个脚本 #vi /usr/local/Monitor_nginx.sh #!/bin/bash if [ "$(ps -ef | grep "ngi ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
- Outlook查找未读邮件
1.查找新邮件的未读邮件,可以在下图中查找 2.恢复已删除邮件,如果邮件是未读邮件,在上图中是查找不到,只能通过视图去查找 步骤2内容摘自百度
- php 画图片
<?php // 使用php操作gd库做图 // 1. 创建一个画布资源 $im = imagecreatetruecolor(80, 40); // 2. 画内容 // 2.1 先位画布准备颜 ...
- Python学习之运算符
Python运算符 算术运算符 运算符 描述 + 相加 - 相减 * 相乘 / 相除 % 取模 ** 幂 // 整除 比较运算符 运算符 描述 == 等于 != 不等于 <> 不等于 &g ...
- iOS 短视频开发总结二 AVCaptureSession
开始录制短视频 录制完成 根据项目需求,对视频URL进行处理 基本原理和方法已经完成,具体细节根据项目实际需求再处理
- 用VC调用EXCEL简单代码(转载自越长大越孤单,觉得很好)
首先在stdafx.h里加入对IDispatch接口提供支持的头文件: #include <afxDisp.h> 再在应用程序类的InitInstance()函数里加入: AfxOleIn ...
- mantis邮箱配置
1.修改/var/www/html/mantisbt-1.3.3/config下config_inc.php配置文件 以163邮箱为例 # --- Email Configuration --- $g ...