첫째, 전반적인 개요
정적 라이브러리의 둘째, 생산
1) 명명 규칙
이름 라이브러리 LIB + + .A
예 : LIB yuchuan의 II.A
2)의 제조 공정은 :
. - .c 인 -> .o를 -C 1)에서는 .o 파일 생성 대응
2하는 것은)가 생성된다. 오 파일 이름 AR RCS + 정적 라이브러리 (libMytest.a) 패키지 + 모든 .o 인에 의해 생성
3) 릴리스 및 정적 라이브러리를 사용합니다
. 1) 정적 라이브러리를 게시
2) 헤더.
4) 장점과 단점 :
셋째, 패키지 정적 라이브러리
A.의 GCC 사용
1. GCC의 컴파일 과정
(1) 전처리 (CPP)의 GCC -E 파일 헤더, 매크로 교체 작업 등을 확대 (일반적으로 가격 요청 .I 출력 종료);
(2) 컴파일러 어셈블리 코드를 생성 (GCC) GCC -S (Q는 .S 종료 가의 출력);
(등) (3) 어셈블러 gcc가 컴파일 된 바이너리 파일로 컴파일 -c (.o 인으로 끝나는 출력 파일);
(4) 커넥터 (LD) GCC, 실행 파일 링크 라이브러리 LIB.
(에서는 hello.c)는 다음과 같이 수행 :
1 #include<stdio.h> 2 3 int main() 4 { 5 printf("hello world\n"); 6 return 0; 7 }
Linux执行过程:
[root@centos1 src]# gcc -E hello.c >> hello.i [root@centos1 src]# gcc -S hello.i [root@centos1 src]# gcc -c hello.s [root@centos1 src]# gcc -o hello.out hello.o [root@centos1 src]# ./hello.out hello world
注:第4步如果使用 ld 会报如下错误
[root@centos1 src]# ld hello.o ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0 hello.o: In function `main': hello.c:(.text+0xa): undefined reference to `puts'
没有 __start 是因为C程序以main为主函数,汇编以start为主函数入口,改用gcc连接就可以,如果非用ld,需要自己链接libc.so库。
2. GCC编译参数
- -I 包含头文件路径(可以使绝对路径,也可以是相对路径)
- -O 优化选项,1-3越高优先级越高
- -L 包含的库路径
- -l(L的小写)指定库名(通常libxxx.so或者libxxx.a,-lxxx)
- -o 目标文件
- -c 编译成.o文件
- -g 用于gdb调试不加此选项不能gdb调试
- -Wall 显示更多的警告
- -D 指定宏编译
- -lstdc++ 编译c++代码
二. 动静库
1. 制作库文件作用
作用:将实现某部分功能的代码封装成库文件,以方便调用,或者是对代码进行保护加密。
应用场景:有时想将某代码提供给别人用,但是又不想公开源代码,这时可以将代码封装成库文件。在开发中调用其他人员编写的库。
2. 静态库
(1)制作步骤
1)编译.o文件
2)将.o文件打包:ar rcs libname.a file1.o file2.o file3.o ...
3)将头文件与库一起发布
文件内容及路径分布如下:
[root@centos1 calc]# tree
.
├── include
│ └── head.h
├── lib
├── main.c
└── src
├── add.c
└── sub.c
1 #include<stdio.h> 2 3 int add(int a, int b); 4 int sub(int a, int b);
1 #include "head.h" 2 3 int add(int a, int b) 4 { 5 return a + b; 6 } 7 8 add.c
1 #include "head.h" 2 3 int sub(int a, int b) 4 { 5 return a - b; 6 } 7 8 sub.c
- 编译为.o文件
进入到 src 目录下执行:
[root@centos1 src]# gcc -c *.c -I ../include/ [root@centos1 src]# ll 总用量 16 -rw-r--r--. 1 root root 63 4月 20 14:53 add.c -rw-r--r--. 1 root root 1240 4月 20 15:10 add.o -rw-r--r--. 1 root root 63 4月 20 14:24 sub.c -rw-r--r--. 1 root root 1240 4月 20 15:10 sub.o
- 将.o文件打包,制作成libCalc.a静态库
[root@centos1 src]# ar rcs libCalc.a *.o [root@centos1 src]# ll 总用量 20 -rw-r--r--. 1 root root 63 4月 20 14:53 add.c -rw-r--r--. 1 root root 1240 4月 20 15:10 add.o -rw-r--r--. 1 root root 2688 4月 20 15:12 libCalc.a -rw-r--r--. 1 root root 63 4月 20 14:24 sub.c -rw-r--r--. 1 root root 1240 4月 20 15:10 sub.o
(2)使用
编译时需要加静态库名(记得路径),-I 包含头文件
- 使用静态库编译main.c并执行main
我们将libCalc.a移动到上层的lib中并退回到calc目录,编译main.c并执行:
[root@centos1 calc]# gcc main.c -o main -I include/ -L ./lib -lCalc [root@centos1 calc]# ll 总用量 24 drwxr-xr-x. 2 root root 4096 4月 20 15:07 include drwxr-xr-x. 2 root root 4096 4月 20 15:14 lib -rwxr-xr-x. 1 root root 6838 4月 20 15:14 main -rw-r--r--. 1 root root 179 4月 20 14:57 main.c drwxr-xr-x. 2 root root 4096 4月 20 15:14 src [root@centos1 calc]# ./main + 10 is 20 - 10 is 0
可以用 nm 命令查看文件内容:
[root@centos1 calc]# nm lib/libCalc.a
add.o:
T add
sub.o:
T sub
(3)静态库优缺点
优点:
1)执行快
2)发布应用时不需要发布库
缺点:
1)执行程序体积会比较大
2)库变更时需要重新编译程序