gcc建立,使用動態庫

2021-06-20 17:23:26 字數 586 閱讀 4977

動態庫在程式啟動時被裝載。在linux下一般是.so檔案。當乙個應用程式裝載了乙個動態庫後,其它應用程式仍可以裝載同乙個動態庫。這個被多程序同時使用的動態庫在記憶體中只有乙個副本。下面演示如何建立,使用動態庫:

原始檔share_lib.c內容如下:

#include void print1() 

void print2()

使用以下命令生成動態庫:

gcc -shared -fpic -o share_lib.so share_lib.c

其中-fpic表示生成位置無關的**,-shared表示將位置無關的**製作為動態庫。

要使用些動態庫,需要建乙個標頭檔案share_lib.h,內容如下:

extern void print1();

extern void print2();

測試檔案main1.c的內容如下:

#include #include "share_lib.h"

int main()

gcc建立和使用靜態庫 動態庫

gcc建立和使用靜態庫 動態庫 下面以工程libtest為例說明gcc 建立和使用 靜態庫 動態庫的過程,libtest目錄結構和內容如圖1所示,其中三個檔案hello.h,hello.c和main.c的內容如下。libtest include hello.h ifdef hello h defin...

gcc建立和使用靜態庫 動態庫

下面以工程libtest為例說明gcc建立和使用靜態庫 動態庫的過程,libtest目錄結構和內容如圖1所示,其中三個檔案hello.h,hello.c和main.c的內容如下。圖1libtest include hello.h ifdef hello h define hello h void h...

cygwin 下使用gcc建立和使用動態庫

參考文章 以及討論帖 摘要 cygwin下gcc只能使用.dll動態庫,不能使用.so型別的動態庫,所有在用gcc建立動態庫的時候,o的引數指定的動態庫的名字需要為lib dll。具體例項 so test.h include stdio.h void test a test a.c include ...