gcc 编译静态链接库

静态链接库(static library)是目标文件(.o文件或.obj文件)的集合,后缀为.a

在编译使用时,静态库会被拷贝到可执行文件中,所以最终生成的可执行文件不依赖于静态库。但这也使得可执行文件庞大,加载速度慢的问题。另外,一旦修改,就必须重新编译,不能想共享库那样灵活的升级。

本文对gcc编辑静态链接库演示一个示例,供参考,好记星不如烂笔头。


1. 编写代码

编写hello.h头文件, 包含print_hello()函数


#ifndef HELLO_H
#define HELLO_H
void print_hello();
#endif
编写hello.c源文件, 包含print_hello函数的实现
#include "hello.h"
#include <stdio.h>
int main(int argc,char *argv[])
{
    printf("hello world!");
}

2. 编译

编译命令(centos5+gcc4.1.2)

[test@hadoop hello]$ gcc -c hello.c  #将源文件编译为目标文件
[test@hadoop hello]$ ls
hello.c  hello.h  hello.o
[test@hadoop hello]$ ar crs libhello.a hello.o #使用ar 将目标文件打包成为.a静态链接库
[test&
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值