1. 程式人生 > >c語言生成靜態庫.a和動態庫.so

c語言生成靜態庫.a和動態庫.so

在windows下動態連結庫是以.dll字尾的檔案,二在Linux中,是以.so作字尾的檔案。
動態連結庫的好處就是節省記憶體空間。


1、Linux下建立動態連結庫
在使用GCC編譯程式時,只需加上-shared選項即可,這樣生成的執行程式即為動態連結庫。
例如有檔案:hello.c x.h main.c
  1. 編譯:gcc hello.c -fPIC -o libhello.so  


其中-fPIC選項的作用是:表示編譯為位置獨立的程式碼,不用此選項的話編譯後的程式碼是位置相關的,
所以動態載入時是通過程式碼拷貝的方式來滿足不同的呼叫,而不能達到真正的程式碼段共享的目的.


將main.c與hello.so動態庫
  1. gcc main.c -L. -lhello -o main  

一、動態連結庫

1.建立hello.so動態庫

  1. #include <stdio.h>
  2. void hello(){  
  3.     printf("hello world\n");  
  4. }  
  5. 編譯:gcc -fPIC -shared hello.c -o libhello.so  

2.hello.h標頭檔案
  1. void hello();  

3.連結動態庫
  1. #include <stdio.h>
  2. #include "hello.h"
  3. int main(){  
  4.     printf("call hello()");  
  5.     hello();  
  6. }  
  7. 編譯:gcc main.c -L. -lhello -o main  
這裡-L的選項是指定編譯器在搜尋動態庫時搜尋的路徑,告訴編譯器hello庫的位置。"."意思是當前路徑.

3.編譯成夠後執行./main,會提示:

  1. In function `main':  
  2. main.c:(.text+0x1d): undefined reference to `hello'  
  3. collect2: ld returned 1 exit status  
這是因為在連結hello動態庫時,編譯器沒有找到。
解決方法:
  1. sudo cp libhello.so /usr/lib/  
這樣,再次執行就成功輸入:
call hello()

二、靜態庫

檔案有:main.c、hello.c、hello.h
1.編譯靜態庫hello.o: 

  1. gcc hello.c -o hello.o  #這裡沒有使用-shared  

2.把目標文件歸檔
  1. ar -r libhello.a hello.o  #這裡的ar相當於tar的作用,將多個目標打包。  
程式ar配合引數-r建立一個新庫libhello.a,並將命令列中列出的檔案打包入其中。這種方法,如果libhello.a已經存在,將會覆蓋現在檔案,否則將新建立。

3.連結靜態庫
  1. gcc main.c -lhello -L. -static -o main  
這裡的-static選項是告訴編譯器,hello是靜態庫。
或者:
  1. gcc main.c libhello.a -L. -o main  
這樣就可以不用加-static

4.執行./main

輸出:call hello()

三、藉助自帶的ldd實現程式來分析動態庫搜尋情況

ldd main

結果:
linux-gate.so.1 =>  (0x00efd000)
libhello.so => /usr/lib/libhello.so (0x00f6b000)
libc.so.6 => /lib/libc.so.6 (0x001a5000)
/lib/ld-linux.so.2 (0x00eb8000)
如果目標程式沒有連結動態庫,則列印“not a dynamic executable”