Linux動態庫函數介紹

來源:互聯網
上載者:User

標頭檔 #include <dlfcn.h>

 

 

載入動態庫

void *dlopen(const char *filename, int flag);

flag的可能值:

l        RTLD_LAZY

l        RTLD_NOW

l        RTLD_GLOBAL

l        RTLD_LOCAL

l        RTLD_NODELETE (since glibc 2.2)

l        RTLD_NOLOAD (since glibc 2.2)

l        RTLD_DEEPBIND

這些flag的具體含義可使用man查看

 

 

返回動態庫中最近的一次錯誤

char *dlerror(void);

 

 

根據動態庫控制代碼和函數名稱,返回函數的地址

void *dlsym(void *handle, const char *symbol);

如果使用dlopen(NULL, <flag>)得到控制代碼,通過這個控制代碼可以尋找所有函數符號,只要那個函數符號所在的dll在載入時指定了RTLD_GLOBAL

 

關閉動態庫

int dlclose(void *handle);

 

根據函數地址,返回函數名稱和其它資訊

注意傳入地址只要落在函數代碼空間範圍之內即可,不需要是函數的起始地址

int dladdr(void *addr, Dl_info *info);

 

typedef struct {

     const char *dli_fname;/* Filename of defining object */

     void *dli_fbase;      /* Load address of that object */

     const char *dli_sname;/* Name of nearest lower symbol */

     void *dli_saddr;      /* Exact value of nearest symbol */

   } Dl_info;

 

 

建構函式和解構函式(這兩個是屬性,而不是函數)

__attribute__((constructor))

__attribute__((destructor))

例如:

__attribute__((constructor)) void before_main()  

{  

       printf("before main/n");  

}  

  

__attribute__((destructor)) void after_main()  

{  

   printf("after main/n");  

}  

 

相關文章

聯繫我們

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