静态库:将库中的二进制映像代码直接拷贝到当前编译的程序中
在Linux中,以*.a 为后缀
使用静态库示例:
#ls
usrhello.c libhello.h libhello.c //用户文件,库头文件,库源文件
#cat libhello.c //查看库源文件内容
#include <stdio.h>
void print_hello(void)
{
printf("hello world,this is library!/n");
}
#cat libhello.h //查看库头文件内容
#ifndef __libhello_H__
#define __libhello_H__
void print_hello(void);
#endif /*__libhello_H__*/
#cat usrhello.c //查看应用程序
#include "libhello.h"
int main()
{
print_hello();
return 0;
}
//开始生成静态库
#gcc -c libhello.c //编译成二进制文件libhello.o
#ar rc libhello.a libhello.o //创建静态库
#gcc -o usrhello_static usrhello.c libhello.a //编译
#./usrhello_static //执行
hello world,this is static_library!
关于ar命令,使用ar命令创建静态库,其中参数:
r:把目标文件放在库中
c:目标文件不存在,默认创建该库