Python调用C++动态库详细步骤(附源码)
在某些项目中需要在python工程中调用自定义的C++的动态库,分为以下两步,第一步:动态库生成;第二步:Python调用动态库。
一:直接调用C++库
第一步:动态库封装(vs2017+qt5.12.10)
1、新建工程时,一定要选择qt下的Qt Class Library 这个选项,如下图所示
2、工程创建成功后,可以在解决方案下看到有testLib.h、testLib.cpp和testlib_global.h三个基础的文件生成,如下图所示
3、下面我们简单写一个打印“vs2017+qt5.12.10生成动态库”的输出函数
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();
};
/*将接口转义供外部调用*/
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生成动态库");
return true;
}
4、下面我们可以编译生成动态库
第二步:Python调用C++动态库
1、打开vs code 并在动态库目录下新建一个test.py文件
test.py
#导入Python调用c++的ctypes库
import ctypes
# 加载dll
my_dll = ctypes.CDLL("Z:/Demo/testLib/x64/Release/testLib.dll")
# 调用dll中的函数
my_dll.PrintfString()
2、通过终端执行Python test.py,如下所示
3、从输出结果可以看到提示加载库错误,这里出现错误的原因是,test.dll依赖的其他的库并没有找到,因此需要使用在test.dll目录下使用windeployqt打包test.dll依赖的库,如下图所示
3、再次在终端执行Python test.py,如下所示,调用动态库打印输出执行成功。
二:调用传递参数C++库
在大多数情况下,我们使用动态库调用时,都涉及参数传递,以下是简单的参数传递列子。
第一步:动态库封装
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 : 参数传递
* @param-in: num:数量,str;字符串
* @return :字符串长度
* @date : 2025-01-01
**************************************************/
int dataInputAndOutput(const int num, const char* str);
};
/*将接口转义供外部调用*/
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
#include "testLib.h"
testLib::testLib()
{
}
bool testLib::PrintfString()
{
/*使用QStringLiteral,否则中文输出会乱码*/
qDebug() << QStringLiteral("vs2017+qt5.12.10生成动态库");
return true;
}
int testLib::dataInputAndOutput(const int num, const char* str)
{
qDebug() << "input num:" << num << "input str:" << str;
return num + strlen(str);
}
4、下面我们可以编译生成动态
第二步:Python调用C++动态库
test.py
#导入Python调用c++的ctypes库
import ctypes
# 加载dll
my_dll = ctypes.CDLL("Z:/Demo/testLib/x64/Release/testLib.dll")
# 调用dll中的函数
my_dll.PrintfString()
str1 = "Z:/Demo/testLib/x64/Release/testLib.dll"
str2 = my_dll.dataInputAndOutput(10,str1.encode("utf-8"))
print(str2);
通过终端执行Python test.py,如下所示
注:如果本文章对您有所帮助,请点赞收藏支持一下,谢谢。^_^
版权声明:本文为博主原创文章,转载请附上博文链接。