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

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

3、下面我們簡單寫一個打印“vs2017+qt5.12.10生成動態(tài)庫”的輸出函數(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 : 說明
* @param-in: 無
* @param-out : 無
* @return : 無
* @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,否則中文輸出會亂碼*/
qDebug() << QStringLiteral("vs2017+qt5.12.10生成動態(tài)庫");
return true;
}4、下面我們可以編譯生成動態(tài)庫

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

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

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

二:調(diào)用傳遞參數(shù)C++庫
在大多數(shù)情況下,我們使用動態(tài)庫調(diào)用時,都涉及參數(shù)傳遞,以下是簡單的參數(shù)傳遞列子。
第一步:動態(tài)庫封裝
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: 無
* @param-out : 無
* @return : 是否成功
* @date : 2025-01-01
**************************************************/
bool PrintfString();
/**************************************************
* @author 鬼魅-9527
* @brief : 參數(shù)傳遞
* @param-in: num:數(shù)量,str;字符串
* @return :字符串長度
* @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: 無
* @param-out : 無
* @return : 是否成功
* @date : 2025-01-01
**************************************************/
bool PrintfString();
/**************************************************
* @author 鬼魅-9527
* @brief : 參數(shù)傳遞
* @param-in: num:數(shù)量,str;字符串
* @return :字符串長度
* @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、下面我們可以編譯生成動態(tài)
第二步:Python調(diào)用C++動態(tài)庫
test.py
#導入Python調(diào)用c++的ctypes庫
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);通過終端執(zhí)行Python test.py,如下所示

到此這篇關(guān)于Python調(diào)用C++動態(tài)庫詳細步驟(附源碼)的文章就介紹到這了,更多相關(guān)Python調(diào)用C++動態(tài)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Python開發(fā)環(huán)境中調(diào)用ChatGPT模型詳細過程
在開發(fā)過程當中時常需要使用 ChatGPT 來完成一些任務,但總是使用網(wǎng)頁交互模式去 Web 端訪問 ChatGPT 是很麻煩的,這時候我們可以使用代碼來調(diào)用 ChatGPT 模型,本文將詳細介紹在 Python 開發(fā)環(huán)境中調(diào)用 ChatGPT 模型過程,,需要的朋友可以參考下2023-05-05
typing.Dict和Dict的區(qū)別及它們在Python中的用途小結(jié)
當在 Python 函數(shù)中聲明一個 dictionary 作為參數(shù)時,我們一般會把 key 和 value 的數(shù)據(jù)類型聲明為全局變量,而不是局部變量。,這篇文章主要介紹了typing.Dict和Dict的區(qū)別及它們在Python中的用途小結(jié),需要的朋友可以參考下2023-06-06
python創(chuàng)建ArcGIS shape文件的實現(xiàn)
今天小編就為大家分享一篇python創(chuàng)建ArcGIS shape文件的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12

