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. 函数库有两种:静态库和动态库. 静态库在程序编译的时候会被连接到目标代码中,程序运行时将不再需要改静态库. 动态库中程序编译的时候并不会连接到目标代码中,而是在程序运行时才被载入,因此在程序运行 ...
随机推荐
- play(1) 第一个简单的应用
去年去了一家公司,公司要求要回使用play,所以在几天的时间内猛学习了一段时间play,发现play!有很多优点:简单,小巧,高开发效率,集成很多功能,容易调试.现在虽然已经不在那家公司,没有使用pl ...
- Exploratory Undersampling for Class-Imbalance Learning
Abstract - Undersampling is a popular method in dealing with class-imbalance problems, which uses on ...
- SQL变量、Substring、charindex、case函数、去除重复
isnull(aa,0)删除表数据: truncate table aaa 添加字段: ALTER TABLE table1 ADD col1 varchar(200) DEFAULT '2008 ...
- HW职责 (Hardware Engineer)
硬件设计就是根据产品经理的需求PRS(Product Requirement Specification),在COGS(Cost of Goods Sale)的要求下,利用目前业界成熟的芯片方案或者技 ...
- The Pragmatic Programmer Quick Reference Guide
1.关心你的技艺 Care About Your Craft 如果不在乎能否漂亮地开发出软件,你又为何要耗费生命去开发软件呢? 2.思考!你的工作 Think! About Your Work 关掉自 ...
- 【JavaScript】之【Object】
见代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- SqlServer类库(自定义)
1,引用System.Configuration; 2,新建应用程序配置文件 读取SqlDataReader中的数据用where (read.Read()){//DoSomeThings()}
- 关于容器为NavigationControlle时,view的起始位置的问题
在iOS 7中,苹果引入了一个新的属性“EdgesForExtendedLayout”,默认值为UIRectEdgeAll,默认的布局将从navigationbar的顶部开始,这就是为什么所有元素都往 ...
- npm(cnpm)介绍
1.npm(node package manager) nodejs的包管理器,用于node插件管理(安装.卸载.更新.管理依赖等); 2.使用npm安装安装插件: 1).命令提示符执行 npm in ...
- 关于Webstorm的一些配置
一:代码缩略图插件:CodeGlance: 二:皮肤与主题使用的sublime的系列风格. 三:实现内嵌JS代码的高亮显示.https://segmentfault.com/q/10100000024 ...