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)文章
Qt中利用QTextBrowser控件設(shè)計(jì)日志窗口
本文主要介紹了Qt中利用QTextBrowser控件設(shè)計(jì)日志窗口,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
華為云開發(fā)工具CodeArts IDE for C/C++開發(fā)使用指南
CodeArts IDE是一個(gè)集成開發(fā)環(huán)境(IDE),它提供了開發(fā)語(yǔ)言和調(diào)試服務(wù),本文主要介紹了華為云開發(fā)工具CodeArts IDE for C/C++ 開發(fā)使用指南,感興趣的可以了解一下2023-08-08
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
C 創(chuàng)建鏈表并將信息存儲(chǔ)在二進(jìn)制文件中讀取的實(shí)例代碼
C 創(chuàng)建鏈表并將信息存儲(chǔ)在二進(jìn)制文件中讀取的實(shí)例代碼,需要的朋友可以參考一下2013-03-03
C++數(shù)據(jù)結(jié)構(gòu)的隊(duì)列詳解
這篇文章主要為大家介紹了C++數(shù)據(jù)結(jié)構(gòu)的隊(duì)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-11-11
C++ Boost PointerContainer智能指針詳解
智能指針是一種像指針的C++對(duì)象,但它能夠在對(duì)象不使用的時(shí)候自己銷毀掉。雖然STL提供了auto_ptr,但是由于不能同容器一起使用(不支持拷貝和賦值操作),因此很少有人使用。它是Boost各組件中,應(yīng)用最為廣泛的一個(gè)2022-11-11

