Python調(diào)用C++動(dòng)態(tài)庫(kù)詳細(xì)步驟(附源碼)
一:直接調(diào)用C++庫(kù)
第一步:動(dòng)態(tài)庫(kù)封裝(vs2017+qt5.12.10)
1、新建工程時(shí),一定要選擇qt下的Qt Class Library 這個(gè)選項(xiàng),如下圖所示

2、工程創(chuàng)建成功后,可以在解決方案下看到有testLib.h、testLib.cpp和testlib_global.h三個(gè)基礎(chǔ)的文件生成,如下圖所示

3、下面我們簡(jiǎn)單寫一個(gè)打印“vs2017+qt5.12.10生成動(dòng)態(tài)庫(kù)”的輸出函數(shù)
testlib_global.h
#pragma once #include <QtCore/qglobal.h> #include <qDebug> #ifndef BUILD_STATIC # if defined(TESTLIB_LIB) # define TESTLIB_EXPORT Q_DECL_EXPORT # else # define TESTLIB_EXPORT Q_DECL_IMPORT # endif #else # define TESTLIB_EXPORT #endif
testLib.h
#pragma once
#include "testlib_global.h"
class TESTLIB_EXPORT testLib
{
public:
testLib();
/**************************************************
* @author 鬼魅-9527
* @brief : 說(shuō)明
* @param-in: 無(wú)
* @param-out : 無(wú)
* @return : 無(wú)
* @date : 2025-01-01
**************************************************/
bool PrintfString();
};
/*將接口轉(zhuǎn)義供外部調(diào)用*/
extern "C"
{
testLib obj;
extern "C" _declspec(dllexport) bool __cdecl PrintfString()
{
return obj.PrintfString();
}
}testLib.cpp
#include "testLib.h"
testLib::testLib()
{
}
bool testLib::PrintfString()
{
/*使用QStringLiteral,否則中文輸出會(huì)亂碼*/
qDebug() << QStringLiteral("vs2017+qt5.12.10生成動(dòng)態(tài)庫(kù)");
return true;
}4、下面我們可以編譯生成動(dòng)態(tài)庫(kù)

第二步:Python調(diào)用C++動(dòng)態(tài)庫(kù)
1、打開vs code 并在動(dòng)態(tài)庫(kù)目錄下新建一個(gè)test.py文件
test.py
#導(dǎo)入Python調(diào)用c++的ctypes庫(kù)
import ctypes
# 加載dll
my_dll = ctypes.CDLL("Z:/Demo/testLib/x64/Release/testLib.dll")
# 調(diào)用dll中的函數(shù)
my_dll.PrintfString()2、通過(guò)終端執(zhí)行Python test.py,如下所示

3、從輸出結(jié)果可以看到提示加載庫(kù)錯(cuò)誤,這里出現(xiàn)錯(cuò)誤的原因是,test.dll依賴的其他的庫(kù)并沒有找到,因此需要使用在test.dll目錄下使用windeployqt打包test.dll依賴的庫(kù),如下圖所示

3、再次在終端執(zhí)行Python test.py,如下所示,調(diào)用動(dòng)態(tài)庫(kù)打印輸出執(zhí)行成功。

二:調(diào)用傳遞參數(shù)C++庫(kù)
在大多數(shù)情況下,我們使用動(dòng)態(tài)庫(kù)調(diào)用時(shí),都涉及參數(shù)傳遞,以下是簡(jiǎn)單的參數(shù)傳遞列子。
第一步:動(dòng)態(tài)庫(kù)封裝
testlib_global.h
#pragma once #include <QtCore/qglobal.h> #include <qDebug> #ifndef BUILD_STATIC # if defined(TESTLIB_LIB) # define TESTLIB_EXPORT Q_DECL_EXPORT # else # define TESTLIB_EXPORT Q_DECL_IMPORT # endif #else # define TESTLIB_EXPORT #endif
testLib.h
#pragma once
#include "testlib_global.h"
class TESTLIB_EXPORT testLib
{
public:
testLib();
/**************************************************
* @author 鬼魅-9527
* @brief : 打印
* @param-in: 無(wú)
* @param-out : 無(wú)
* @return : 是否成功
* @date : 2025-01-01
**************************************************/
bool PrintfString();
/**************************************************
* @author 鬼魅-9527
* @brief : 參數(shù)傳遞
* @param-in: num:數(shù)量,str;字符串
* @return :字符串長(zhǎng)度
* @date : 2025-01-01
**************************************************/
int dataInputAndOutput(const int num, const char* str);
};
/*將接口轉(zhuǎn)義供外部調(diào)用*/
extern "C"
{
testLib obj;
extern "C" _declspec(dllexport) bool __cdecl PrintfString()
{
return obj.PrintfString();
}extern "C" _declspec(dllexport) int __cdecl dataInputAndOutput(const int num, const char* str)
{
return obj.dataInputAndOutput(num, str);
}
}testLib.cpp
#pragma once
#include "testlib_global.h"
class TESTLIB_EXPORT testLib
{
public:
testLib();
/**************************************************
* @author 鬼魅-9527
* @brief : 打印
* @param-in: 無(wú)
* @param-out : 無(wú)
* @return : 是否成功
* @date : 2025-01-01
**************************************************/
bool PrintfString();
/**************************************************
* @author 鬼魅-9527
* @brief : 參數(shù)傳遞
* @param-in: num:數(shù)量,str;字符串
* @return :字符串長(zhǎng)度
* @date : 2025-01-01
**************************************************/
int dataInputAndOutput(const int num, const char* str);
};
/*將接口轉(zhuǎn)義供外部調(diào)用*/
extern "C"
{
testLib obj;
extern "C" _declspec(dllexport) bool __cdecl PrintfString()
{
return obj.PrintfString();
}extern "C" _declspec(dllexport) int __cdecl dataInputAndOutput(const int num, const char* str)
{
return obj.dataInputAndOutput(num, str);
}
}4、下面我們可以編譯生成動(dòng)態(tài)
第二步:Python調(diào)用C++動(dòng)態(tài)庫(kù)
test.py
#導(dǎo)入Python調(diào)用c++的ctypes庫(kù)
import ctypes
# 加載dll
my_dll = ctypes.CDLL("Z:/Demo/testLib/x64/Release/testLib.dll")
# 調(diào)用dll中的函數(shù)
my_dll.PrintfString()
str1 = "Z:/Demo/testLib/x64/Release/testLib.dll"
str2 = my_dll.dataInputAndOutput(10,str1.encode("utf-8"))
print(str2);通過(guò)終端執(zhí)行Python test.py,如下所示

到此這篇關(guān)于Python調(diào)用C++動(dòng)態(tài)庫(kù)詳細(xì)步驟(附源碼)的文章就介紹到這了,更多相關(guān)Python調(diào)用C++動(dòng)態(tài)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)圖的廣度和深度優(yōu)先路徑搜索算法
圖是一種抽象數(shù)據(jù)結(jié)構(gòu),本質(zhì)和樹結(jié)構(gòu)是一樣的。圖與樹相比較,圖具有封閉性,可以把樹結(jié)構(gòu)看成是圖結(jié)構(gòu)的前生。本文將利用Python實(shí)現(xiàn)圖的廣度和深度優(yōu)先路徑搜索算法,感興趣的可以學(xué)習(xí)一下2022-04-04
PyQt5爬取12306車票信息程序的實(shí)現(xiàn)
12306是學(xué)習(xí)爬蟲的比較好的一個(gè)練手網(wǎng)站。本文主要實(shí)現(xiàn)了PyQt5爬取12306車票信息程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
提升Python Web應(yīng)用性能的10個(gè)關(guān)鍵技巧
Python作為一種強(qiáng)大的編程語(yǔ)言,在Web開發(fā)領(lǐng)域也有著廣泛的應(yīng)用,通過(guò)結(jié)合Python的靈活性和一些高性能的框架和工具,我們可以構(gòu)建出高性能的Web應(yīng)用程序,本文將介紹一些關(guān)鍵的技術(shù)和方法,幫助你在Python環(huán)境下構(gòu)建高性能的Web應(yīng)用程序,需要的朋友可以參考下2024-07-07
用Python實(shí)現(xiàn)流星雨效果的方法詳解
這篇文章主要為大家介紹了Python實(shí)現(xiàn)流星雨效果的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>2021-12-12
Python判斷一個(gè)目錄下哪些是文件哪些是文件夾的方法
本文介紹Python中識(shí)別目錄文件及文件夾的完整方案,支持跨平臺(tái)路徑處理、文件屬性分析(類型/大小/時(shí)間)、遞歸掃描、統(tǒng)計(jì)匯總,并提供彩色樹形輸出與錯(cuò)誤處理功能,滿足多樣化需求,需要的朋友可以參考下2025-09-09

