Linux GCC編譯使用動態、靜態連結庫

來源:互聯網
上載者:User

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

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

編譯:gcc hello.c -fPIC -o libhello.so

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

將main.c與hello.so動態庫

gcc main.c -L. -lhello -o main

一、動態連結程式庫

1.建立hello.so動態庫

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

2.hello.h標頭檔

void hello();

3.連結動態庫

#include <stdio.h>#include "hello.h"int main(){printf("call hello()");hello();}編譯:gcc main.c -L. -lhello -o main

這裡-L的選項是指定編譯器在搜尋動態庫時搜尋的路徑,告訴編譯器hello庫的位置。"."意思是當前路徑.

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

In function `main': main.c:(.text+0x1d): undefined reference to `hello'collect2: ld returned 1 exit status

這是因為在連結hello動態庫時,編譯器沒有找到。
解決方案:

sudo cp libhello.so /usr/lib/

這樣,再次執行就成功輸入:
call hello()

二、靜態庫

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

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

2.把目的文件歸檔

ar -r libhello.a hello.o  #這裡的ar相當於tar的作用,將多個目標打包。

程式ar配合參數-r建立一個新庫libhello.a,並將命令列中列出的檔案打包入其中。這種方法,如果libhello.a已經存在,將會覆蓋現在檔案,否則將新建立。

3.連結靜態庫

gcc main.c -lhello -L. -static -o main

這裡的-static選項是告訴編譯器,hello是靜態庫。
或者:

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”

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.