Linux第乙個動態庫

2022-06-13 08:09:09 字數 1051 閱讀 2843

動態庫一般以.so結尾,就是shared object的意思.

其基本生成步驟為   ⑴編寫函式**   ⑵編譯生成動態庫檔案,要加上 -shared 和 -fpic 選項 ,

庫檔名以lib開頭, 以.so 結尾。     -fpic 使輸出的物件模組是按照可重定位位址方式生成的。-shared指定把對應的原始檔生成對應的動態鏈結庫檔案libstr.so檔案。使用方式分為兩種: 隱式呼叫和顯示呼叫  隱式呼叫類似於靜態庫的使用,但需修改動態鏈結庫的配置檔案/etc/ld.so.conf;  顯示呼叫則是在主程式裡使用dlopen、dlsym、dlerror、dlclose等系統函式。

第乙個簡單的動態庫編寫:

-----------func.h----------- 

#include

void showhello();

----------func.c-----------

#include "func.h"

void showhello()

gcc -fpic -shared -o libtest2.so  func.c   生成 動態庫 libtest2.so

動態庫顯示呼叫:

------------uselib.c-----------------------

#include

#include "func.h" 

#include

int main()

char* errch=dlerror();

if(null!=errch)

pfun=dlsym(pdhandle,"showhello");

errch=dlerror();

if(null!=errch)

(*pfun)();

dlclose(pdhandle);

return 0;

}通過命令  gcc -o uselib -ldl uselib.c  編譯生成可執行檔案 uselib    -ldl  選項表示 用到了共享庫

./uselib  執行 可列印出    show hello func called  

Linux第乙個動態庫

動態庫一般以.so結尾,就是shared object的意思.其基本生成步驟為 編寫函式 編譯生成動態庫檔案,要加上 shared 和 fpic 選項 庫檔名以lib開頭,以.so 結尾。fpic 使輸出的物件模組是按照可重定位位址方式生成的。shared指定把對應的原始檔生成對應的動態鏈結庫檔案l...

第乙個linux 驅動

以前看過很多次linux相關的資料,一直沒親自動手寫,今天通過半天努力,終於完成了乙個自己的linux小驅動 hello.c include include module license dual bsd gpl static int hello init void static void hell...

python第乙個程式設計 第乙個 Python 程式

簡述 安裝完 python 後,windows 中 開始選單或安裝目錄下就會有 idle 開發 python 程式的基本 ide 整合開發環境 幫助手冊 模組文件等。linux 中 只需要在命令列中輸入 python 命令即可啟動互動式程式設計。互動式程式設計 互動式程式設計不需要建立指令碼檔案,是...