linux動態(tài)鏈接庫使用方法分享
1、前言
在實際開發(fā)過程中,各個模塊之間會涉及到一些通用的功能,比如讀寫文件,查找、排序。為了減少代碼的冗余,提高代碼的質(zhì)量,可以將這些通用的部分提取出來,做出公共的模塊庫。通過動態(tài)鏈接庫可以實現(xiàn)多個模塊之間共享公共的函數(shù)。之前看《程序員的自我修養(yǎng)》中講到程序的鏈接和裝入過程,這些玩意都是底層的,對于理解程序的編譯過程有好處。http://www.ibm.com/developerworks/cn/linux/l-dynlink/博文介紹了程序的鏈接和裝入過程。本文重點在于應用,如何編寫和使用動態(tài)鏈接庫,后續(xù)使用動態(tài)鏈接庫實現(xiàn)一個插件程序。
2、動態(tài)鏈接庫生產(chǎn)
動態(tài)鏈接庫與普通的程序相比而言,沒有main函數(shù),是一系列函數(shù)的實現(xiàn)。通過shared和fPIC編譯參數(shù)生產(chǎn)so動態(tài)鏈接庫文件。程序在調(diào)用庫函數(shù)時,只需要連接上這個庫即可。例如下面實現(xiàn)一個簡單的整數(shù)四則運輸?shù)膭討B(tài)鏈接庫,定義的caculate.h和caculate.c兩個文件,生產(chǎn)libcac.so動態(tài)鏈接庫。
程序代碼如下:
/*caculate.h*/
#ifndef CACULATE_HEAD_
#define CACULATE_HEAD_
//加法
int add(int a, int b);
//減法
int sub(int a, int b);
//除法
int div(int a, int b);
//乘法
int mul(int a, int b);
#endif
/*caculate.c文件*/
#include "caculate.h"
//求兩個數(shù)的和
int add(int a, int b)
{
return (a + b);
}
//減法
int sub(int a, int b)
{
return (a - b);
}
//除法
int div(int a, int b)
{
return (int)(a / b);
}
//乘法
int mul(int a, int b)
{
return (a * b);
}
編譯生產(chǎn)libcac.so文件如下: gcc -shared -fPIC caculate.c -o libcac.so
編寫一個測試程序調(diào)用此動態(tài)鏈接庫的函數(shù),程序如下所示:
#include <stdio.h>
#include "caculate.h"
int main()
{
int a = 20;
int b = 10;
printf("%d + %d = %d\n", a, b, add(a, b));
printf("%d - %d = %d\n", a, b, sub(a, b));
printf("%d / %d = %d\n", a, b, div(a, b));
printf("%d * %d = %d\n", a, b, mul(a, b));
return 0;
}
編譯生產(chǎn)可執(zhí)行文件main如下:gcc main.c -o main -L ./ -lcac (其中-L指明動態(tài)鏈接庫的路徑,-l后是鏈接庫的名稱,省略lib)
程序執(zhí)行結果如下所示:
3、獲取動態(tài)鏈接庫的函數(shù)
linux提供dlopen、dlsym、dlerror和dlcolose函數(shù)獲取動態(tài)鏈接庫的函數(shù)。通過這個四個函數(shù)可以實現(xiàn)一個插件程序,方便程序的擴展和維護。函數(shù)格式如下所示:
#include <dlfcn.h>
void *dlopen(const char *filename, int flag);
char *dlerror(void);
void *dlsym(void *handle, const char *symbol);
int dlclose(void *handle);
Link with -ldl.
dlopen()是一個強大的庫函數(shù)。該函數(shù)將打開一個新庫,并把它裝入內(nèi)存。該函數(shù)主要用來加載庫中的符號,這些符號在編譯的時候是不知道的。寫個測試程序調(diào)用上面生產(chǎn)libcac.so庫如下所示:
#include <stdio.h>
#include <dlfcn.h>
#define DLL_FILE_NAME "libcac.so"
int main()
{
void *handle;
int (*func)(int, int);
char *error;
int a = 30;
int b = 5;
handle = dlopen(DLL_FILE_NAME, RTLD_NOW);
if (handle == NULL)
{
fprintf(stderr, "Failed to open libaray %s error:%s\n", DLL_FILE_NAME, dlerror());
return -1;
}
func = dlsym(handle, "add");
printf("%d + %d = %d\n", a, b, func(a, b));
func = dlsym(handle, "sub");
printf("%d + %d = %d\n", a, b, func(a, b));
func = dlsym(handle, "div");
printf("%d + %d = %d\n", a, b, func(a, b));
func = dlsym(handle, "mul");
printf("%d + %d = %d\n", a, b, func(a, b));
dlclose(handle);
return 0;
}
程序執(zhí)行結果如下所示:gcc call_main.c -o call_main -ldl
相關文章
Shell腳本實現(xiàn)的基于SVN的代碼提交量統(tǒng)計工具
這篇文章主要介紹了Shell腳本實現(xiàn)的基于SVN的代碼提交量統(tǒng)計工具,本文直接給出實現(xiàn)腳本代碼,需要的朋友可以參考下2015-06-06Shell腳本實現(xiàn)監(jiān)控kingate并自動啟動
這篇文章主要介紹了Shell腳本實現(xiàn)監(jiān)控kingate并自動啟動,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2014-12-12shell腳本監(jiān)控linux系統(tǒng)內(nèi)存使用情況的方法(不使用nagios監(jiān)控linux)
在沒有nagios監(jiān)控軟件的情況下,只要服務器能上互聯(lián)網(wǎng),就可通過發(fā)郵件的方式來提醒管理員系統(tǒng)內(nèi)存的使用情況2014-02-02Linux下使用nextcloud搭建個人網(wǎng)盤代碼實例
這篇文章主要介紹了Linux下使用nextcloud搭建個人網(wǎng)盤代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,,需要的朋友可以參考下2019-06-06詳解systemctl?和?service?區(qū)別及命令
systemctl和service都是管理Linux系統(tǒng)服務的工具,但systemctl更加先進,可以方便地管理systemd服務,而service適用于管理傳統(tǒng)的SysV服務,這篇文章主要介紹了systemctl和service區(qū)別及命令,需要的朋友可以參考下2023-07-07shell腳本中使用iconv實現(xiàn)批量文件轉(zhuǎn)碼的代碼分享
這篇文章主要介紹了shell腳本中使用iconv實現(xiàn)批量文件轉(zhuǎn)碼的代碼分享,本文提供了2個實現(xiàn)代碼和各自使用方法,需要的朋友可以參考下2014-08-08