前提知識:
動態庫尾碼.so 靜態庫尾碼.a
-c 只編譯不連結
-o 指定輸出名
-L 指定連結庫的尋找路徑
-l 指定連結的庫名
-I 檔案裡麵包含了標頭檔,-I後面跟著尋找標頭檔的路徑
上面幾項可以滿足編譯目標
產生.a檔案
命令ar
q:快速追加。增加新模組到庫的結尾處
t 查看.a檔案裡麵包含的成員
x 提取出.a檔案裡的成員,如果不指定提取的成員,將全部提取(註:不刪除歸檔檔案裡的成員)
例:
G2DBT1:/wxp/so_a#ar t libhu.a
libhu.o
cmaps.o
sccsid.o
libhu_r.o
libhu32_r.o
libhu32.o
G2DBT1:/wxp/so_a#ar x libhu.a libhu.o
G2DBT1:/wxp/so_a#ls
libhu.a libhu.o
d 刪除模組中的成員
例:
G2DBT1:/wxp/so_a#ar d libhu.a libhu.o
G2DBT1:/wxp/so_a#ar t libhu.a
cmaps.o
sccsid.o
libhu_r.o
libhu32_r.o
libhu32.o
m 移動模組中成員的順序,配合a(移動到指定成員的後面) b(移動到指定模組的前面) 來使用,如果不指定移動的位置,預設為最後面
G2DBT1:/wxp/so_a#ar -mb cmaps.o libhu.a libhu.o
G2DBT1:/wxp/so_a#ar t libhu.a
libhu.o
cmaps.o
sccsid.o
libhu_r.o
libhu32_r.o
libhu32.o
v 列出執行過程中的附加資訊
據以上得出如果建立一個.a檔案,命令如下:
/enpc/wxp/so_a$cc -c sayhello.c
/enpc/wxp/so_a$ls
hello.c sayhello.c sayhello.o
/enpc/wxp/so_a$ar -rv libhello.a sayhello.o
ar: Creating an archive file libhello.a.
a - sayhello.o
/enpc/wxp/so_a$ls
hello.c libhello.a sayhello.c sayhello.o
函數調用.a的命令
/enpc/wxp/so_a$cc hello.c -L./ -lhello -o sayhello
/enpc/wxp/so_a$ls
hello.c libhello.a sayhello sayhello.c sayhello.o
/enpc/wxp/so_a$./sayhello
say hello
/enpc/wxp/so_a$ldd sayhello
sayhello needs:
/usr/lib/libc.a(shr.o)
/unix
/usr/lib/libcrypt.a(shr.o)
ldd的命令裡面居然沒有libhello.a檔案
靜態檔案,有沒有或許都不重要!
生產一個動態檔案:
使用makefile檔案來生產靜態庫並調用成功
該檔案還不是很成功
clean:
-rm *.o
-rm *.a
all:main.o libhello.a
cc -o all main.o -L./ -lhello
main.o:hello.c
cc -c hello.c -o main.o
libhello.a:sayhello.o
ar -rv libhello.a sayhello.o
sayhello.o:sayhello.c
cc -c sayhello.c -o sayhello.o