linux 下静态库的创建及使用

1.创建静态库

 [xpan@localhost 2.5.2]$ ls
libhello.c  libhello.h  usehello.c
[xpan@localhost 2.5.2]$ cat libhello.h
#ifndef _libhello_H_
#define _libhello_H_
void print_hello(void);
#endif /*_libhello_H_*/
[xpan@localhost 2.5.2]$ cat libhello.c
#include<stdio.h>
void print_hello(void)
{
  printf("hello world ,this is library\n");
}

[xpan@localhost 2.5.2]$ gcc -c libhello.c
[xpan@localhost 2.5.2]$ ls libhello.o
libhello.o
[xpan@localhost 2.5.2]$ ar rc libhello.a libhello.o

参数:

r: 把目标文件包含在库中,替换任何已经在档案中存在的同名目标文件;

c: 如果目标文件不存在,则默认创建该库;

s: 维护映射符号名到目标文件的表格;

 


[xpan@localhost 2.5.2]$ ls libhello.a
libhello.a
[xpan@localhost 2.5.2]$ file libhello.a
libhello.a: current ar archive

 

2.使用静态库

[xpan@localhost 2.5.2]$ cat usehello.c
#include "libhello.h"
/*hello*/
int main(void)
{
  print_hello();
  return 0;
}
[xpan@localhost 2.5.2]$ gcc -o usehello_static usehello.c libhello.a
[xpan@localhost 2.5.2]$ ./usehello_static
hello world ,this is library
[xpan@localhost 2.5.2]$



转载于:https://www.cnblogs.com/panxihua/archive/2012/06/11/2544503.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值