AIX環境下,調用動態庫以及靜態庫

來源:互聯網
上載者:User

前提知識:

動態庫尾碼.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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.