一、前言
1、上一篇文章,我们已经对C/C++工程进行了二次封装,并生成了可用的python模块
2、本篇文章将基于上一篇文章的思路,自己写一个python模块
C++开发实战(四):对提供接口的C/C++进行二次开发_Lion King的博客-CSDN博客_c++二次开发 https://blog.csdn.net/weixin_43431593/article/details/121615034?spm=1001.2014.3001.5501
二、写一个只有函数对象的python模块
1、创建工程(动态库项目,windows动态库)
![](http://image109.360doc.com/DownloadImg/2023/10/1416/273798793_2_20231014041818710.png)
![](http://image109.360doc.com/DownloadImg/2023/10/1416/273798793_3_20231014041818992.png)
![](http://image109.360doc.com/DownloadImg/2023/10/1416/273798793_4_20231014041819179.png)
2、 初步配置开发环境
![](http://image109.360doc.com/DownloadImg/2023/10/1416/273798793_5_20231014041819570.png)
![](http://image109.360doc.com/DownloadImg/2023/10/1416/273798793_6_20231014041819867.png)
![](http://image109.360doc.com/DownloadImg/2023/10/1416/273798793_7_20231014041820179.png)
![](http://image109.360doc.com/DownloadImg/2023/10/1416/273798793_8_20231014041820523.png)
![](http://image109.360doc.com/DownloadImg/2023/10/1416/273798793_9_20231014041820867.png)
![](http://image109.360doc.com/DownloadImg/2023/10/1416/273798793_10_2023101404182170.png)
3、编写程序
dllmain.cpp
// dllmain.cpp : 定义 DLL 应用程序的入口点。 BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, switch (ul_reason_for_call) PyObject* cpp_sum(PyObject*, PyObject* args) printf("%s(%d):%s\r\n", __FILE__, __LINE__, __FUNCTION__); // 打印日志 int num = 0; // 用于接收传入的整型数值 PyArg_ParseTuple(args, "i", &num); // python元组,解析传入的参数 long long sum = 0; // 设置输出结果为长整形 for (int i = 1; i <= num; i++) printf("%s(%d):%s\r\n", __FILE__, __LINE__, __FUNCTION__); // 打印日志 return Py_BuildValue("L", sum); // 返回长整型数值 PyMethodDef onedllMethods[] = {"sum", cpp_sum, METH_VARARGS, "sum 1 to num!"}, // 方法名称(供外部调用),函数本体(C函数),可变参数,方法说明 {NULL, NULL, 0, NULL} // 表示结尾,全员为空 struct PyModuleDef onedllModule = PyModuleDef_HEAD_INIT, // 固定写法 "onedll", // 与模块名称(项目名称)一致 "onedll python test module!", // 模块说明,help该模块时给出的提示信息,不要用中文 PyMODINIT_FUNC // 返回python函数对象 PyInit_onedll() // 命名一定要加上工程名称,即PyInit_工程名(模块名) return PyModule_Create(&onedllModule/*模块信息*/);
4、编译输出
(1)配置输出python库的文件类型
![](http://pubimage.360doc.com/wz/default.gif)
(2)编译
![](http://pubimage.360doc.com/wz/default.gif)
(3)使用编译好的python库
![](http://pubimage.360doc.com/wz/default.gif)
![](http://pubimage.360doc.com/wz/default.gif)
![](http://pubimage.360doc.com/wz/default.gif)
5、问题解决
(1)python不是debug版本,没有debug的东西,因此报如下错误:
Fatal Python error: _PyInterpreterState_GET: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL) Python runtime state: unknown (2)以”初步配置开发环境“的方式配置Release环境
![](http://pubimage.360doc.com/wz/default.gif)
![](http://pubimage.360doc.com/wz/default.gif)
![](http://pubimage.360doc.com/wz/default.gif)
6、遗留一个问题:模块迁移到其他环境产生的问题
(1)在其他电脑的python环境,导入该模块,提示找不到指定模块,原因是导入DLL失败,说明咋们的这个模块不完整或python版本不兼容,先放着,后续再解决。
>>> import onedll Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: DLL load failed while importing onedll: 找不到指定的模块。
![](http://pubimage.360doc.com/wz/default.gif)
三、写一个包含类对象的python模块
1、编写程序
dllmain.cpp
// dllmain.cpp : 定义 DLL 应用程序的入口点。 #include <string> // 增加字符串,供类使用 using namespace std; // 增加命名空间 void CppClassDel(PyObject* obj); // 声明一个被调用的函数 BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, switch (ul_reason_for_call) printf("%s(%d):%s\r\n", __FILE__, __LINE__, __FUNCTION__); printf("%s(%d):%s\r\n", __FILE__, __LINE__, __FUNCTION__); double test(const string& s) { printf("%s(%d):%s <%s>\r\n", __FILE__, __LINE__, __FUNCTION__, s.c_str()); PyObject* CppClassInit(PyObject*, PyObject* args) printf("%s(%d):%s\r\n", __FILE__, __LINE__, __FUNCTION__); return PyCapsule_New(new CppClass, "CppClass", CppClassDel); void CppClassDel(PyObject* obj) printf("%s(%d):%s\r\n", __FILE__, __LINE__, __FUNCTION__); CppClass* tmp = (CppClass*)PyCapsule_GetPointer(obj, "CppClass"); PyObject* CppClassTest(PyObject*, PyObject* args) { char* pstr = (char*)str.c_str(); PyArg_ParseTuple(args, "Os", &thiz, &pstr); CppClass* _this = (CppClass*)PyCapsule_GetPointer(thiz, "CppClass"); double ret = _this->test(str); return Py_BuildValue("d", ret); PyObject* cpp_sum(PyObject*, PyObject* args) printf("%s(%d):%s\r\n", __FILE__, __LINE__, __FUNCTION__); // 打印日志 int num = 0; // 用于接收传入的整型数值 PyArg_ParseTuple(args, "i", &num); // python元组,解析传入的参数 long long sum = 0; // 设置输出结果为长整形 for (int i = 1; i <= num; i++) printf("%s(%d):%s\r\n", __FILE__, __LINE__, __FUNCTION__); // 打印日志 return Py_BuildValue("L", sum); // 返回长整型数值 PyMethodDef onedllMethods[] = {"sum", cpp_sum, METH_VARARGS, "sum 1 to num!"}, // 方法名称(供外部调用),函数本体(C函数),可变参数,方法说明 {"CppClassInit", CppClassInit, METH_VARARGS, "CppClass()"}, {"CppClassTest", CppClassTest, METH_VARARGS, "double Test(string str)"}, {NULL, NULL, 0, NULL} // 表示结尾,全员为空 struct PyModuleDef onedllModule = PyModuleDef_HEAD_INIT, // 固定写法 "onedll", // 与模块名称(项目名称)一致 "onedll python test module!", // 模块说明,help该模块时给出的提示信息,不要用中文 PyMODINIT_FUNC // 返回python函数对象 PyInit_onedll() // 命名一定要加上工程名称,即PyInit_工程名(模块名) return PyModule_Create(&onedllModule/*模块信息*/);
2、编写测试程序
run_test.py
return CppClassTest(self.thiz, s)
3、遗留一个问题:python传入的字符串没有被打印出来
![](http://pubimage.360doc.com/wz/default.gif)
|