多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## 静态库 静态库,也叫归档文件(archive),因此按惯例得到`.a`结尾后缀的库文件(比如`/usr/lib/libc.a`) 自己制作静态库:基于`ar`程序和`gcc -c`(编译、汇编到目标代码,不进行链接)对函数分别进行编译,应该尽可能的分别保存到不同的源文件中,公共数据保存在同一个源文件中(基于声明的静态变量) ### 自己创建静态库 <details> <summary>fred.c</summary> ``` #include <stdio.h> void fred(int arg) { printf("fred: we passwd %d \n", arg); } ``` </details> <br/> <details> <summary>bill.c</summary> ``` #include <stdio.h> void bill(char *arg) { printf("bill: we passwd %s\n", arg); } ``` </details> <br/> <details> <summary>bf.h</summary> ``` #include <stdlib.h> #include "bf.h" int main() { bill("hey man, cool"); exit(0); } ``` </details> <br/> <details> <summary>porg.c</summary> ``` #include <stdlib.h> #include "bf.h" int main() { bill("hey man, cool"); exit(0); } ``` </details> <br/> 命令行链接 & 打包 ``` gcc -c *.c #编译成object,用于链接和做成静态库 gcc -o prog prog.o bill.o #链接输出到prog执行文件 // 制作静态库 ar crv libfoo.a bill.o fred.o #创建库文件 ranlib libfoo.a #为函数库生成内容表,后续可以基于`nm libfoo.a`或者`nm prog`查看 // 编译链接 gcc -o prog2 prog.c -L. -lfoo // 或者 gcc -o prog1 prog.c -L. libfoo.a ```