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)單寫(xiě)一個(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、打開(kāi)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依賴(lài)的其他的庫(kù)并沒(méi)有找到,因此需要使用在test.dll目錄下使用windeployqt打包test.dll依賴(lài)的庫(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 pytesseract驗(yàn)證碼識(shí)別庫(kù)用法解析
這篇文章主要介紹了Python pytesseract驗(yàn)證碼識(shí)別庫(kù)用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
在Python開(kāi)發(fā)環(huán)境中調(diào)用ChatGPT模型詳細(xì)過(guò)程
在開(kāi)發(fā)過(guò)程當(dāng)中時(shí)常需要使用 ChatGPT 來(lái)完成一些任務(wù),但總是使用網(wǎng)頁(yè)交互模式去 Web 端訪問(wèn) ChatGPT 是很麻煩的,這時(shí)候我們可以使用代碼來(lái)調(diào)用 ChatGPT 模型,本文將詳細(xì)介紹在 Python 開(kāi)發(fā)環(huán)境中調(diào)用 ChatGPT 模型過(guò)程,,需要的朋友可以參考下2023-05-05
typing.Dict和Dict的區(qū)別及它們?cè)赑ython中的用途小結(jié)
當(dāng)在 Python 函數(shù)中聲明一個(gè) dictionary 作為參數(shù)時(shí),我們一般會(huì)把 key 和 value 的數(shù)據(jù)類(lèi)型聲明為全局變量,而不是局部變量。,這篇文章主要介紹了typing.Dict和Dict的區(qū)別及它們?cè)赑ython中的用途小結(jié),需要的朋友可以參考下2023-06-06
Pandas中如何對(duì)DataFrame列名進(jìn)行重命名
在做數(shù)據(jù)挖掘的時(shí)候,想改一個(gè)DataFrame的column名稱(chēng),所以就查了一下,下面這篇文章主要給大家介紹了關(guān)于Pandas中如何對(duì)DataFrame列名進(jìn)行重命名的相關(guān)資料,需要的朋友可以參考下2023-04-04
python創(chuàng)建ArcGIS shape文件的實(shí)現(xiàn)
今天小編就為大家分享一篇python創(chuàng)建ArcGIS shape文件的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
python 實(shí)現(xiàn)倒計(jì)時(shí)功能(gui界面)
這篇文章主要介紹了python 實(shí)現(xiàn)倒計(jì)時(shí)功能(gui界面),幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11

