靜態庫:將庫中的二進制映像代碼直接拷貝到當前編譯的程序中
在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:目標文件不存在,默認創建該庫