源代碼文件在編譯之後生成相應的目標文件(.o文件),通常制作靜態庫的過程就是將這些.o文件打包成一個libXXXX.a文件。
ar命令常用的幾個命令選項的含義如下:
//2.c
#include<stdio.h>
void func2() {
printf("call func2.\n");
}
做如下操作:
[[email protected] ar]$ ls
1.c 2.c
[[email protected] ar]$ gcc -c 1.c 2.c
[[email protected] ar]$ ls
1.c 1.o 2.c 2.o
[[email protected] ar]$ ar -r libtest.a 1.o
ar: creating libtest.a
[[email protected] ar]$ ar -r libtest.a 2.o
[[email protected] ar]$ ar -t libtest.a
1.o
2.o
[[email protected] ar]$ ar -d libtest.a 1.o
[[email protected] ar]$ ar -t libtest.a
2.o
[[email protected] ar]$ ar -r libtest.a 1.o
[[email protected] ar]$ rm -rf 1.o 2.o
[[email protected] ar]$ ls
1.c 2.c libtest.a
[[email protected] ar]$ ar -x libtest.a
[[email protected] ar]$ ls
1.c 1.o 2.c 2.o libtest.a
[[email protected] ar]$ nm -s libtest.a
Archive index:
func1 in 1.o
func2 in 2.o
1.o:
0000000000000000 T func1
U puts
2.o:
0000000000000000 T func2
U puts
[[email protected] ar]$