Linux系統共用庫編程- linux shared library 編程入門

來源:互聯網
上載者:User

Linux系統共用庫編程

作者:蘇懷廣

 

一、說明

  類似Windows系統中的動態連結程式庫,Linux中也有相應的共用庫用以支援代碼的複用。Windows中為*.dll,而Linux中為*.so。下面詳細介紹如何建立、使用Linux的共用庫。

二、建立共用庫

在mytestso.c檔案中,代碼如下:

#include <stdio.h>
#include <stdlib.h>
int GetMax(int a, int b)
{
if (a >= b)
return a;
return b;
}
int GetInt(char* psztxt)
{
if (0 == psztxt)
return -1;
return atoi(psztxt);
}

然後使用下列命令進行編譯:

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

-fpic 使輸出的對象模組是按照可重定位地址方式產生的

編譯成功後,目前的目錄下有mytestso.so,此時已成功建立共用庫mytestso.so。

三、使用共用庫

  共用庫中的函數可被主程式載入並執行,但是不必編譯時間連結到主程式的目標檔案中。主程式使用共用庫中的函數時,需要事Crowdsourced Security Testing道所包含的函數的名稱(字串),然後根據其名稱獲得該函數的起始地址(函數指標),然後即可使用該函數指標使用該函數。

在mytest.c檔案中,代碼如下:

#include <dlfcn.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
void* pdlhandle;
char* pszerror;
int (*GetMax)(int a, int b);
int (*GetInt)(char* psztxt);
int a, b;
char* psztxt = "1024";
// open mytestso.so
pdlhandle = dlopen("./mytestso.so", RTLD_LAZY);
pszerror = dlerror();
if (0 != pszerror) {
printf("%s"n", pszerror);
exit(1);
}
// get GetMax func
GetMax = dlsym(pdlhandle, "GetMax");
pszerror = dlerror();
if (0 != pszerror) {
printf("%s"n", pszerror);
exit(1);
}
// get GetInt func
GetInt = dlsym(pdlhandle, "GetInt");
pszerror = dlerror();
if (0 != pszerror) {
printf("%s"n", pszerror);
exit(1);
}
// call fun
a = 200;
b = 600;
printf("max=%d"n", GetMax(a, b));
printf("txt=%d"n", GetInt(psztxt));
// close mytestso.so
dlclose(pdlhandle);
}

然後使用如下命令進行編譯:

 

gcc mytest.c -ldl -o mytest

-ldl選項,表示產生的對象模組需要使用共用庫

(1)dlopen()

第一個參數:指定共用庫的名稱,將會在下面位置尋找指定的共用庫。

-環境變數LD_LIBRARY_PATH列出的用分號間隔的所有目錄。

-檔案/etc/ld.so.cache中找到的庫的列表,用ldconfig維護。

-目錄usr/lib。

-目錄/lib。

-目前的目錄。

第二個參數:指定如何開啟共用庫。

-RTLD_NOW:將共用庫中的所有函數載入到記憶體

-RTLD_LAZY:會推後共用庫中的函數的載入操作,直到調用dlsym()時方載入某函數

(2)dlsym()

調用dlsym時,利用dlopen()返回的共用庫的phandle以及函數名稱作為參數,返回要載入函數的入口地址。

(3)dlerror()

該函數用於檢查調用共用庫的相關函數出現的錯誤。

四、結束語

本文主要闡述了Linux系統使用共用庫的編程問題,並通過一個簡單的執行個體詳細說明。

相關文章

聯繫我們

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