[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创建静态库的更多相关文章

  1. Linux 创建静态库(.a)和动态库(.so)

    0. 回顾一下 gcc 选项 ============================================== -E : 仅做预处理,例如去注释,宏展开,include 展开等 -S : ...

  2. Linux 创建静态库.a

    gcc -c 只编译不连接 -o *.o(生成.o文件) ar crv  name.a   *.o *.o  (ar 命令把 .o文件打包成  name.a 静态库) 测试 name.a -L 紧跟链 ...

  3. linux下静态库的制作

     在我们编写软件的过程当中,少不了需要使用别人的库函数.因为大家知道,软件是一个协作的工程.作为个人来讲,你不可能一个人完成所有的工作.另外,网络上一些优秀的开源库已经被业内广泛接受,我们也没有必要把 ...

  4. Linux命令之ar - 创建静态库.a文件和动态库.so

    转自:http://blog.csdn.net/eastonwoo/article/details/8241693 用途说明 创建静态库.a文件.用C/C++开发程序时经常用到,但我很少单独在命令行中 ...

  5. 在Linux中创建静态库.a和动态库.so

    转自:http://www.cnblogs.com/laojie4321/archive/2012/03/28/2421056.html 在Linux中创建静态库.a和动态库.so 我们通常把一些公用 ...

  6. 在Linux中创建静态库和动态库

    我们通常把一些公用函数制作成函数库,供其它程序使用. 函数库分为静态库和动态库两种. 静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库. 动态库在程序编译时并不会被连接到目标代码中 ...

  7. 在Linux中创建静态库和动态库 (转)

    我们通常把一些公用函数制作成函数库,供其它程序使用.函数库分为静态库和动态库两种.静态 库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.动态库在程序编译时并不会被连接到目标代码中,而 ...

  8. zt:我使用过的Linux命令之ar - 创建静态库.a文件

    我使用过的Linux命令之ar - 创建静态库.a文件 本文链接:http://codingstandards.iteye.com/blog/1142358    (转载请注明出处) 用途说明 创建静 ...

  9. linux中创建静态库和动态库

    1. 函数库有两种:静态库和动态库. 静态库在程序编译的时候会被连接到目标代码中,程序运行时将不再需要改静态库. 动态库中程序编译的时候并不会连接到目标代码中,而是在程序运行时才被载入,因此在程序运行 ...

随机推荐

  1. 使用Volley执行网络数据传输

    首先需要实例化一个RequestQueue RequestQueue queue = Volley.newRequestQueue(this); 然后是根据提供的URL请求字符串响应 String u ...

  2. 使用MJRefresh遇到的一个问题,上拉刷新后tableview瞬间滑到最底部

    最近用MJRefresh上拉刷新时遇到一个问题,就是上拉刷新后,tableview会瞬间滑到最底部,用户还要往回翻才能看到新刷出来的数据,体验十分不好.查了很久没找到原因,最后发现在refreshvi ...

  3. 启动Mysql服务提示Can’t connect to local MySQL server through socket的解决方法

    启动Mysql服务常会提示下面错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/ ...

  4. .Net中使用com组件后发生System.ArithmeticException异常的解决办法(Message=算术运算中发生溢出或下溢。)

    最近在开发一个.Net程序,其中涉及到对com组件的调用,或者第三方DLL调用, 在调用完以后如果使用一些小的测试程序继续运行,一切正常,但是在使用带有GUI的form程序,或者WPF程序中,继续执行 ...

  5. 【python】属性

    在python中,一切皆是对象(object),对象拥有很多属性(arrtribute) 属性分2种 类属性(class attribute):类自身定义or继承 对象属性(object attrib ...

  6. Spark MLlib 之 Naive Bayes

    1.前言: Naive Bayes(朴素贝叶斯)是一个简单的多类分类算法,该算法的前提是假设各特征之间是相互独立的.Naive Bayes 训练主要是为每一个特征,在给定的标签的条件下,计算每个特征在 ...

  7. 点击按钮跳出隐藏的div层,并设背景。

    style: .black_overlay{ display: none; position: absolute; top: 0%; left: 0%; width: 100%; height: 10 ...

  8. C#读写文件的方法汇总_C#教程_脚本之家

    C#读写文件的方法汇总_C#教程_脚本之家 http://www.jb51.net/article/34936.htm

  9. 什么是java?

    Java是一种软件技术  是一种由美国SUN计算机公司(Sun Microsystems, Inc.)所研究而成的语言  是一种为 Internet发展的计算机语言  是一种使网页(Web Page) ...

  10. iOS AVCaptureSession 小视频开发总结,支持设备旋转

    iOS开发中当我们想要自定义相机拍照或摄像界面时,UIImagePickerController无法满足我们的需求,这时候我们可以使用AVFoundation.framework这个framework ...