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. 函数库有两种:静态库和动态库. 静态库在程序编译的时候会被连接到目标代码中,程序运行时将不再需要改静态库. 动态库中程序编译的时候并不会连接到目标代码中,而是在程序运行时才被载入,因此在程序运行 ...
随机推荐
- MFC-01-Chapter01:Hello,MFC---1.1 Windows 编程模型
1.1 Windows编程模型 为传统的操作系统编写的程序使用的是过程化模型,即程序从头到尾按顺序执行.例如C程序,从main函数入口开始执行,中间调用不同的函数一直到程序结束返回,这种过程是程序本身 ...
- NET出现频率非常高的笔试题
又到了金三银四的跳槽季,许多朋友又开始跳槽了,这里我简单整理了一些出现频率比较高的.NET笔试题,希望对广大求职者有所帮助. 一..net基础 1. a=10,b=15,请在不使用第三方变量的情况下 ...
- myeclipse激活法,可以试一试
我的myeclipse2014也是这样激活: 第一步:输入任意用户名 第二步:点击Systemid... 按钮,自动生成本机器的systemid. 第三步: 点菜单Tools->RebuildK ...
- selenium+python+eclipse开发中遇到的问题
1.中文编码问题 报错提示:SyntaxError: Non-ASCII character '\xba' in file D:\autotest\PythonCase\src\selenium\te ...
- C# WebBrowser控件使用教程与技巧收集
常用的方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(strin ...
- 第三章 DOM
节点的概念 5个常用的DOM方法:getElementById.getElementsByTagName.getElementsByClassName.getAttribute.getAttribut ...
- reinstall ubuntu
flickering mouse issue http://askubuntu.com/questions/310341/do-graphics-drivers-for-intel-hd-4600-e ...
- linux内核分析——扒开系统调用的三层皮(上)
20135125陈智威 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 系统调用 ...
- php 队列
一.php中对共享内存,消息队列的操作 http://blog.csdn.net/haitun312366/article/details/8614797 二.PHP memcache 队列类 htt ...
- Linux文件系统层次结构标准
该标准的目的是定义Linux文件系统的标准路径,使得开发者和用户可以在合理的位置找到需要的东西. Linux的文件布局的大体想法是将文件和目录分为如下3组: 对运行Linux的某一特定系统唯一的文件和 ...