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. 函数库有两种:静态库和动态库. 静态库在程序编译的时候会被连接到目标代码中,程序运行时将不再需要改静态库. 动态库中程序编译的时候并不会连接到目标代码中,而是在程序运行时才被载入,因此在程序运行 ...
随机推荐
- Fix the iOS code signing issue when using Jenkins
This week I setup the Jenkins on my Mac and try to build iOS applications. unfortunately I got the c ...
- google 账号登陆chrome内容是中文的问题
最近要用到google accout使用某项服务,奇怪的是之前是英文,登陆后就显示中文了,我把浏览器默认的语言和电脑的系统语言改了也无济于事,最好还是google 帮忙解决了,原来我的google a ...
- 在IIS Express中调试时无法读取配置文件 错误
在IIS Express中调试代码时,如果出现"无法读取配置文件"的问题(如图),这种情况是IIS Express的"applicationhost.config&quo ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
- 利用python实现爬虫爬取某招聘网站,北京地区岗位名称包含某关键字的所有岗位平均月薪
#通过输入的关键字,爬取北京地区某岗位的平均月薪 # -*- coding: utf-8 -*- import re import requests import time import lxml.h ...
- CMD的几个常用API
一: define, 全局函数,用来定义模块. 参数: 1.id 模块标识(可省略) 2.deps模块依赖(比如jquery)(可省略) 3.factory:可能是: (1)对象 (2)字符串 ①和 ...
- JAVA中管道通讯(线程间通讯)例子
Java I/O系统是建立在数据流概念之上的,而在UNIX/Linux中有一个类似的概念,就是管道,它具有将一个程序的输出当作另一个程序的输入的能力.在Java中,可以使用管道流进行线程之间的通信,输 ...
- beacon帧
1.MAC头部 解释: ① Version 版本号 目前为止802.11只有一个版本,所以协议编号为0 ② Type 00表示管理帧,01表示控制帧,10表示数据帧 ③ Subtype 和Type一 ...
- C语言程序设计第11次作业
一.本次课主要内容: 本章主要介绍指针相关的基础知识,本节课的主要如下 (1)通过示例"密码开锁"引入指针的概念和主要知识点,分析了密码开锁的过程来说明变量.内存单元和地址之间的关 ...
- Python学习之运算符
Python运算符 算术运算符 运算符 描述 + 相加 - 相减 * 相乘 / 相除 % 取模 ** 幂 // 整除 比较运算符 运算符 描述 == 等于 != 不等于 <> 不等于 &g ...