制作步驟
- 編譯為 .o 文件
createliba目錄下
結構為
├── head
│ └── test.h
├── jia.c
├── jian.c
├── cheng.c
├── chu.c
這4個源文件,提供 + - * /算數運算, 使用了 head目錄下 test.h文件
先編譯為.o文件
gcc -c *.c -I./head
ls
cheng.c cheng.o chu.c chu.o head jia.c jian.c jian.o jia.o
- 將 .o文件 打包
ar rcs libxxx.a file1.o file2.o file3.o ...
比如我做的實驗是:
ar rcs libjjcc.a *.o
- 將頭文件與庫一起發布
這里我把頭文件 head/test.h 與步驟2生成的libjjcc.a
cp到testliba目錄下
└── testliba├── app├── libjjcc.a├── main.c└── test.h
cat main.c
#include "test.h"
int main() {int a = jia(3, 5);printf("3 + 5 = %d", a);
}
編譯main.c
gcc main.c -o app -L ./ -l jjcc (其中-L,指定庫目錄, -l是指定庫名)
生成app可執行文件
nm libxxx.a 查看打包的靜態庫