linux创建静态库
[1]新建源程序staticlib.c
/*************************************************************************
> File Name: staticlib.c
> Author: copener
> Mail: hanmingye@foxmail.com
> Created Time: 2015年05月13日 星期三 17时08分11秒
************************************************************************/ /*sum*/
int add(unsigned int a, unsigned int b){
return (a+b);
} /*sub*/
int sub(unsigned int a, unsigned int b){
return (a-b);
} /*mul*/
int mul(unsigned int a, unsigned int b){
return (a*b);
} /*div*/
int div(unsigned int a, unsigned int b){
return (a/b);
}
[2]编译但不链接源码staticlib.c 生成staticlib.o
oee@copener:~/workspace/test/staticlib$ ls
staticlib.c
oee@copener:~/workspace/test/staticlib$ gcc -c staticlib.c
oee@copener:~/workspace/test/staticlib$ ls
staticlib.c staticlib.o
[3]生成静态库文件staticlib.a
//注:r表示加入,若库不存在则用c参数创建,s表示把添加的内容更新到库文件
oee@copener:~/workspace/test/staticlib$ ar rcs staticlib.a staticlib.o
oee@copener:~/workspace/test/staticlib$ ls
staticlib.a staticlib.c staticlib.o
[4]添加staticlib.h的头文件引用
/*************************************************************************
> File Name: staticlib.h
> Author: copener
> Mail: hanmingye@foxmail.com
> Created Time: 2015年05月13日 星期三 17时10分42秒
************************************************************************/ extern int add(unsigned int a, unsigned int b);
extern int sub(unsigned int a, unsigned int b);
extern int mul(unsigned int a, unsigned int b);
extern int div(unsigned int a, unsigned int b);
[5]新建程序testapp.c调用静态库函数测试
/*************************************************************************
> File Name: testapp.c
> Author: copener
> Mail: hanmingye@foxmail.com
> Created Time: 2015年05月13日 星期三 17时15分47秒
************************************************************************/ #include<stdio.h>
#include"staticlib.h" /*包含库函数接口*/ int main(void){
unsigned int a,b;
printf("please input a and b:\r\n");
scanf("%d%d",&a,&b); printf("#########################################\r\n");
printf("#add :%d\r\n",add(a,b));
printf("#sub :%d\r\n",sub(a,b));
printf("#mul :%d\r\n",mul(a,b));
printf("#div :%d\r\n",div(a,b));
printf("#########################################\r\n"); return ;
}
[6]编译测试源码链接静态staticlib.a,生成testapp可执行程序
oee@copener:~/workspace/test/staticlib$ gcc -c testapp.c -o testapp.o
oee@copener:~/workspace/test/staticlib$ ls
staticlib.a staticlib.c staticlib.h staticlib.o testapp.c testapp.o
oee@copener:~/workspace/test/staticlib$ gcc testapp.o ./staticlib.a -o testapp
oee@copener:~/workspace/test/staticlib$ ls
staticlib.a staticlib.c staticlib.h staticlib.o testapp testapp.c testapp.o
[7]测试运行testapp
oee@copener:~/workspace/test/staticlib$ ./testapp
please input a and b: #########################################
#add :
#sub :
#mul :
#div :
#########################################
[8]小结:库写好后,只要提供staticlib.a库和staticlib.h函数头文件给使用者即可。链接库在编译程序时要放在被编译的源码文件之后,否则可能会链接失败。
linux创建静态库的更多相关文章
- Linux 创建静态库(.a)和动态库(.so)
0. 回顾一下 gcc 选项 ============================================== -E : 仅做预处理,例如去注释,宏展开,include 展开等 -S : ...
- Linux 创建静态库.a
gcc -c 只编译不连接 -o *.o(生成.o文件) ar crv name.a *.o *.o (ar 命令把 .o文件打包成 name.a 静态库) 测试 name.a -L 紧跟链 ...
- linux下静态库的制作
在我们编写软件的过程当中,少不了需要使用别人的库函数.因为大家知道,软件是一个协作的工程.作为个人来讲,你不可能一个人完成所有的工作.另外,网络上一些优秀的开源库已经被业内广泛接受,我们也没有必要把 ...
- Linux命令之ar - 创建静态库.a文件和动态库.so
转自:http://blog.csdn.net/eastonwoo/article/details/8241693 用途说明 创建静态库.a文件.用C/C++开发程序时经常用到,但我很少单独在命令行中 ...
- 在Linux中创建静态库.a和动态库.so
转自:http://www.cnblogs.com/laojie4321/archive/2012/03/28/2421056.html 在Linux中创建静态库.a和动态库.so 我们通常把一些公用 ...
- 在Linux中创建静态库和动态库
我们通常把一些公用函数制作成函数库,供其它程序使用. 函数库分为静态库和动态库两种. 静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库. 动态库在程序编译时并不会被连接到目标代码中 ...
- 在Linux中创建静态库和动态库 (转)
我们通常把一些公用函数制作成函数库,供其它程序使用.函数库分为静态库和动态库两种.静态 库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.动态库在程序编译时并不会被连接到目标代码中,而 ...
- zt:我使用过的Linux命令之ar - 创建静态库.a文件
我使用过的Linux命令之ar - 创建静态库.a文件 本文链接:http://codingstandards.iteye.com/blog/1142358 (转载请注明出处) 用途说明 创建静 ...
- linux中创建静态库和动态库
1. 函数库有两种:静态库和动态库. 静态库在程序编译的时候会被连接到目标代码中,程序运行时将不再需要改静态库. 动态库中程序编译的时候并不会连接到目标代码中,而是在程序运行时才被载入,因此在程序运行 ...
随机推荐
- 使用Volley执行网络数据传输
首先需要实例化一个RequestQueue RequestQueue queue = Volley.newRequestQueue(this); 然后是根据提供的URL请求字符串响应 String u ...
- 【转】tomcat性能调优
一.总结前一天的学习 从"第三天"的性能测试一节中,我们得知了决定性能测试的几个重要指标,它们是: ü 吞吐量 ü Responsetime ü Cpuload ü ...
- css3实现头像旋转功能(超easy!!!)
简单好玩的头像旋转功能 html结构 <body> <img src="https://a-ssl.duitang.com/uploads/item/201604/29/2 ...
- Command Pattern
当(客户)对象访问(服务)请求服务时,最直接的方法就是方法调用.
- 当你刷新当前Table时,刷新后如何回到你上一次所在位置呢?
第一: 在你刷新前保存所在位置的行号 procedure XXXClass.LockPositionEx;begin DisableControls; FHistoryRecNo := 0; FHis ...
- PyAutoGUI-python版的autoit/AHK
简单介绍各个图形界面自动操作的python库,类似按键精灵\autoit\ahk(autohotkey)等等这些自动化工具.这类python库不是只是用来实现自动游戏之类的程序,业界也用这些库来做GU ...
- xtrabackup: error: last checkpoint LSN (3409281307) is larger than last copied LSN (3409274368)
1.错误发生场景:使用2.4.1版本的xtrabackup工具进行全备,备份日志中报出此错误2.知识要点:MySQL中,redo 日志写进程会在三种条件下被触发从log buffer中写日志到redo ...
- 微软云创益大赛获奖团队风采:做一个中国特色的.Net源代码社区
为了强化云技术,落地云应用,彰显云价值,微软(中国)携手中国计算机报举办了“微软Cloud OS第二届云创益大赛”.本届大赛历时111天,共吸引了6647位个人组选手回答了70,078道题,59支参赛 ...
- 使用xib创建cell时 bug
UITableView (<UITableView: 0x15799a800; frame = (0 4797; 375 733); clipsToBounds = YES; tag = 305 ...
- php大力力 [049节] php函数implode()
implode()[1] 函数返回一个由数组元素组合成的字符串. 注释:implode() 函数接受两种参数顺序.但是由于历史原因,explode() 是不行的,您必须保证 separator 参数 ...