C++動(dòng)態(tài)調(diào)用動(dòng)態(tài)鏈接庫(kù)(DLL或SO)的方法實(shí)現(xiàn)
動(dòng)態(tài)庫(kù)調(diào)用流程大致可以描述為:0.創(chuàng)建動(dòng)態(tài)庫(kù) -> 1.加載動(dòng)態(tài)庫(kù) -> 2.定義函數(shù)類型 -> 3.獲取函數(shù)地址 -> 4.調(diào)用函數(shù) -> 5.卸載動(dòng)態(tài)庫(kù)。
這個(gè)流程和邏輯可以在不同的操作系統(tǒng)和編譯器下略有差異,因此需要根據(jù)特定的平臺(tái)和工具鏈做適當(dāng)?shù)恼{(diào)整。
0.創(chuàng)建動(dòng)態(tài)庫(kù):
創(chuàng)建一個(gè)接口,并生成可供其他程序使用的DLL和SO(動(dòng)態(tài)鏈接庫(kù)),步驟如下:
要?jiǎng)?chuàng)建一個(gè)接口,并生成可供其他程序使用的DLL和SO(動(dòng)態(tài)鏈接庫(kù)),可以按照以下步驟進(jìn)行:
- 創(chuàng)建接口頭文件(例如
helloFunc.h
):在頭文件中定義接口的函數(shù)和數(shù)據(jù)結(jié)構(gòu)。將接口的所有公共部分放在這個(gè)頭文件中,并確保使用適當(dāng)?shù)膶?dǎo)出聲明。
// helloFunc.h #ifdef _MSC_VER // Windows環(huán)境下的導(dǎo)出聲明 #ifdef helloFunc_EXPORTS #define HELLOFUNC_API __declspec(dllexport) #else #define HELLOFUNC_API __declspec(dllimport) #endif #else // Linux/Unix下的導(dǎo)出聲明 #ifdef HELLOFUNC_EXPORTS #define HELLOFUNC_API __attribute__((visibility("default"))) #else #define HELLOFUNC_API #endif #endif // 接口函數(shù) #ifdef __cplusplus extern "C" { #endif HELLOFUNC_API void hello(); #ifdef __cplusplus } #endif
- 實(shí)現(xiàn)接口函數(shù)的源文件(例如
helloFunc.cpp
):在源文件中實(shí)現(xiàn)接口函數(shù)。確保使用正確的導(dǎo)出聲明,并根據(jù)需要處理接口的具體邏輯。
// helloFunc.cpp #include "helloFunc.h" void hello() { // 實(shí)現(xiàn)函數(shù)邏輯 // ... }
- 生成 DLL 和 SO:
- 在Windows環(huán)境下,可以使用 Visual Studio 或 MinGW 等工具來(lái)生成 DLL 文件。將接口頭文件和實(shí)現(xiàn)源文件添加到工程中,并設(shè)置導(dǎo)出選項(xiàng),編譯工程以生成 DLL 文件。
- 在Linux/Unix環(huán)境下,可以使用 GCC 或 Clang 等編譯器來(lái)生成 SO 文件。將接口頭文件和實(shí)現(xiàn)源文件編譯成目標(biāo)文件,然后使用編譯器的特定選項(xiàng)和命令來(lái)將目標(biāo)文件鏈接成 SO 文件。
1. 加載動(dòng)態(tài)庫(kù):
使用操作系統(tǒng)提供的函數(shù)(如LoadLibrary()
、dlopen()
)加載動(dòng)態(tài)庫(kù)文件。需要指定動(dòng)態(tài)庫(kù)的文件路徑或名稱。
#if defined (WIN32) | defined (WIN64) HMODULE handle = nullptr; // 動(dòng)態(tài)庫(kù)句柄 #else void* handle = nullptr; // 動(dòng)態(tài)庫(kù)句柄 #endif // 加載動(dòng)態(tài)庫(kù) #if defined (WIN32) | defined (WIN64) handle = LoadLibrary("example.dll"); // 在Windows中使用 #else handle = dlopen("libexample.so", RTLD_LAZY); // 在Linux/Unix中使用 #endif if (!handle) { #if defined (WIN32) | defined (WIN64) std::cerr << "無(wú)法加載動(dòng)態(tài)庫(kù): " << GetLastError() << std::endl; // 在Windows中使用 #else std::cerr << "無(wú)法加載動(dòng)態(tài)庫(kù): " << dlerror() << std::endl; #endif return 1; }
2. 函數(shù)類型定義:
使用函數(shù)指針來(lái)定義函數(shù)的類型,以便在動(dòng)態(tài)庫(kù)中找到的函數(shù)能夠正確地調(diào)用。函數(shù)指針的類型必須與函數(shù)的簽名(參數(shù)類型和返回類型)匹配。
void (*helloFunc)(); // 函數(shù)指針
3. 獲取函數(shù)地址:
通過(guò)使用操作系統(tǒng)提供的函數(shù)(如GetProcAddress()
、dlsym()
)獲取特定函數(shù)的地址。需要指定要調(diào)用的函數(shù)的名稱。
// 獲取函數(shù)地址 #if defined (WIN32) | defined (WIN64) helloFunc = (void (*)())GetProcAddress(handle, "hello"); // 在Windows中使用 #else helloFunc = (void (*)())dlsym(handle, "hello"); // 在Linux/Unix中使用 #endif if (helloFunc == nullptr) { #if defined (WIN32) | defined (WIN64) std::cerr << "無(wú)法找到函數(shù): " << GetLastError() << std::endl; // 在Windows中使用 FreeLibrary(handle); // 在Windows中使用 #else std::cerr << "無(wú)法找到函數(shù): " << dlerror() << std::endl; dlclose(handle); // 在Linux/Unix中使用 #endif return 1; }
4. 調(diào)用函數(shù):
通過(guò)調(diào)用函數(shù)指針,實(shí)現(xiàn)對(duì)動(dòng)態(tài)庫(kù)中函數(shù)的調(diào)用。根據(jù)函數(shù)的參數(shù)類型和返回類型,在適當(dāng)?shù)奈恢脗鬟f參數(shù),并根據(jù)需要處理返回值。
// 調(diào)用函數(shù) helloFunc();
5. 卸載動(dòng)態(tài)庫(kù):
使用操作系統(tǒng)提供的函數(shù)(如FreeLibrary()
、dlclose()
)卸載已加載的動(dòng)態(tài)庫(kù)。通常在不再需要?jiǎng)討B(tài)庫(kù)時(shí)執(zhí)行這個(gè)步驟。
// 卸載動(dòng)態(tài)庫(kù) #if defined (WIN32) | defined (WIN64) FreeLibrary(handle); // 在Windows中使用 #else dlclose(handle); // 在Linux/Unix中使用 #endif
6.總結(jié)
- helloFunc.h
// helloFunc.h #ifdef _MSC_VER // Windows環(huán)境下的導(dǎo)出聲明 #ifdef helloFunc_EXPORTS #define HELLOFUNC_API __declspec(dllexport) #else #define HELLOFUNC_API __declspec(dllimport) #endif #else // Linux/Unix下的導(dǎo)出聲明 #ifdef HELLOFUNC_EXPORTS #define HELLOFUNC_API __attribute__((visibility("default"))) #else #define HELLOFUNC_API #endif #endif // 接口函數(shù) #ifdef __cplusplus extern "C" { #endif HELLOFUNC_API void hello(); #ifdef __cplusplus } #endif
- helloFunc.cpp
// helloFunc.cpp #include "helloFunc.h" void hello() { // 實(shí)現(xiàn)函數(shù)邏輯 // ... }
- main.cpp
#include <iostream> #if defined (WIN32) | defined (WIN64) #include <windows.h> // 在Windows中使用 #else #include <dlfcn.h> // 在Linux/Unix中使用 #endif int main() { #if defined (WIN32) | defined (WIN64) HMODULE handle = nullptr; // 動(dòng)態(tài)庫(kù)句柄 #else void* handle = nullptr; // 動(dòng)態(tài)庫(kù)句柄 #endif // 1. 加載動(dòng)態(tài)庫(kù) #if defined (WIN32) | defined (WIN64) handle = LoadLibrary("example.dll"); // 在Windows中使用 #else handle = dlopen("libexample.so", RTLD_LAZY); // 在Linux/Unix中使用 #endif if (!handle) { #if defined (WIN32) | defined (WIN64) std::cerr << "無(wú)法加載動(dòng)態(tài)庫(kù): " << GetLastError() << std::endl; // 在Windows中使用 #else std::cerr << "無(wú)法加載動(dòng)態(tài)庫(kù): " << dlerror() << std::endl; #endif return 1; } // 2. 函數(shù)類型定義 void (*helloFunc)(); // 函數(shù)指針 // 3. 獲取函數(shù)地址 #if defined (WIN32) | defined (WIN64) helloFunc = (void (*)())GetProcAddress(handle, "hello"); // 在Windows中使用 #else helloFunc = (void (*)())dlsym(handle, "hello"); // 在Linux/Unix中使用 #endif if (helloFunc == nullptr) { #if defined (WIN32) | defined (WIN64) std::cerr << "無(wú)法找到函數(shù): " << GetLastError() << std::endl; // 在Windows中使用 FreeLibrary(handle); // 在Windows中使用 #else std::cerr << "無(wú)法找到函數(shù): " << dlerror() << std::endl; dlclose(handle); // 在Linux/Unix中使用 #endif return 1; } // 4. 調(diào)用函數(shù) helloFunc(); // 5. 卸載動(dòng)態(tài)庫(kù) #if defined (WIN32) | defined (WIN64) FreeLibrary(handle); // 在Windows中使用 #else dlclose(handle); // 在Linux/Unix中使用 #endif return 0; }
到此這篇關(guān)于C++動(dòng)態(tài)調(diào)用動(dòng)態(tài)鏈接庫(kù)(DLL或SO)的文章就介紹到這了,更多相關(guān)C++動(dòng)態(tài)調(diào)用動(dòng)態(tài)鏈接庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ boost::asio編程-異步TCP詳解及實(shí)例代碼
這篇文章主要介紹了C++ boost::asio編程-異步TCP詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11C 程序?qū)崿F(xiàn)密碼隱秘輸入的實(shí)例 linux系統(tǒng)可執(zhí)行
下面小編就為大家?guī)?lái)一篇C 程序?qū)崿F(xiàn)密碼隱秘輸入的實(shí)例 linux系統(tǒng)可執(zhí)行。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Qt圖片繪圖類之QPixmap/QImage/QPicture詳解
這篇文章主要為大家詳細(xì)介紹了Qt圖片繪圖類中QPixmap、QImage和QPicture的使用方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03C語(yǔ)言中結(jié)構(gòu)體的內(nèi)存對(duì)齊規(guī)則講解
C 數(shù)組允許定義可存儲(chǔ)相同類型數(shù)據(jù)項(xiàng)的變量,結(jié)構(gòu)是 C 編程中另一種用戶自定義的可用的數(shù)據(jù)類型,它允許你存儲(chǔ)不同類型的數(shù)據(jù)項(xiàng),本篇讓我們來(lái)了解C 的結(jié)構(gòu)體內(nèi)存對(duì)齊2022-05-05詳解應(yīng)用程序與驅(qū)動(dòng)程序通信DeviceIoControl
這種通信方式,就是驅(qū)動(dòng)程序和應(yīng)用程序自定義一種IO控制碼,然后調(diào)用DeviceIoControl函數(shù),IO管理器會(huì)產(chǎn)生一個(gè)MajorFunction為IRP_MJ_DEVICE_CONTROL,MinorFunction為自己定義的控制碼的IRP,系統(tǒng)就調(diào)用相應(yīng)的處理IRP_MJ_DEVICE_CONTROL的派遣函數(shù)2021-06-06C++實(shí)現(xiàn)教職工信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)教職工信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C/C++實(shí)現(xiàn)快速排序算法的兩種方式實(shí)例
快速排序是一種采用分治思想,在實(shí)踐中通常運(yùn)行較快一種排序算法,這篇文章主要給大家介紹了關(guān)于C/C++實(shí)現(xiàn)快速排序的兩種方式的相關(guān)資料,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考下2021-08-08