python与c++混合编程

上传人:第*** 文档编号:32819150 上传时间:2018-02-12 格式:DOC 页数:10 大小:3.80MB
返回 下载 相关 举报
python与c++混合编程_第1页
第1页 / 共10页
python与c++混合编程_第2页
第2页 / 共10页
python与c++混合编程_第3页
第3页 / 共10页
python与c++混合编程_第4页
第4页 / 共10页
python与c++混合编程_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《python与c++混合编程》由会员分享,可在线阅读,更多相关《python与c++混合编程(10页珍藏版)》请在金锄头文库上搜索。

1、普通方式1、环境搭建1. 安装 python2.6(python3.xx 版本编译失败,未找到原因) 。2. 配置 vs2008 项目(python 自带的 python26.dll 为 release 版本,所以 vs2008 项目也必须调整为 release,但是通过设置也可以对 release 进行调试,如果需要 debug 版本可以从网上下载已有的 python26_d.dll 和 python26_d.lib,也可以下载 python 源码自己编译。 )1C/C+ - 常规 - 附件库包含目录,加入 python 头文件目录,如下图:2连接器 - 常规 -附件库目录。加入 pytho

2、n 库文件目录,如下图:3连接器 - 输入 - 附加依赖项。加入 python 库文件名,如下图:2、代码实现1. C+调用 python1高层次嵌入 python(所谓的高层次嵌入主要是指程序与脚本间没有交互)#include int main()Py_Initialize(); /* Python 解释器初始化 */PyRun_SimpleString(print hi,python!); /* 运行 python 字符串 */Py_Finalize(); /* 结束 Python 解释器,释放资源 */return 0;2低层次嵌入 pyton,可以交互。但是只限 python 的基本类

3、型数据(整型、浮点型、字符串、元组、列表和字典) 。C+代码:#include #include int main(int argc, char* argv) PyObject *modulename, *module, *dic, *func, *args, *rel, *list;char *funcname1 = sum;int i;Py_ssize_t s;printf(-=在 C 中嵌入 Python=-n);/* Python 解释器的初始化*/Py_Initialize(); if(!Py_IsInitialized() printf(初始化失败 !); return -1; /

4、* 导入 Python 模块,并检验是否正确导入 */modulename = Py_BuildValue(s, pytest);/*pytest 参数,为 python 脚本,此处的意思是导入当前路径下名称为 pytest.py 的 python 脚本文件 */module = PyImport_Import(modulename); if(!module) printf(导入 pytest 失败!); return -1; /* 获得模块中函数并检验其有效性 */dic = PyModule_GetDict(module);if(!dic)printf(错误 !n);return -1;

5、/* 获得 sum 函数地址并验证 */func = PyDict_GetItemString(dic,funcname1); if(!PyCallable_Check(func) printf(不能找到函数 %s,funcname1); return -1; /* 构建列表 */list = PyList_New(5);printf(使用 Python 中的 sum 函数求解下列数之和n);for (i = 0; i 常规 - 附件库包含目录,加入 boost 头文件目录,如下图:5连接器 - 常规 -附件库目录。加入 boost 库文件目录,如下图:6连接器 - 输入 - 附加依赖项。加入

6、 boost.python 库文件名,如下图:2、代码实现1. C+调用 python(利用 boos.Python 实现 C+调用 python,实现数据结构(如基本类型,字符串,整数类型等,以及自定义的类等类型)间的转换)构造一个 c+的对象,传入到 python 中,并在 python 的脚本中调用其函数C+代码:#include #include using namespace boost:python;class Worldpublic:void set(std:string msg) this-msg = msg; std:string greet() return msg; st

7、d:string msg;typedef boost:shared_ptr world_ptr;BOOST_PYTHON_MODULE(hello)class_ (World).def(greet, &World:greet).def(set, &World:set);register_ptr_to_python ();int main(int argc, char *argv) Py_Initialize();world_ptr worldObjectPtr (new World);worldObjectPtr-set(Hello from C+!);try inithello();PyRu

8、n_SimpleString(import hello);object module(handle (borrowed(PyImport_AddModule(_main_);object dictionary = module.attr(_dict_);dictionarypyWorldObjectPtr = worldObjectPtr;PyRun_SimpleString(pyWorldObjectPtr.set(Hello from Python!); catch (error_already_set) PyErr_Print();std:cout greet(): greet() us

9、ing namespace boost:python;BOOST_PYTHON_MODULE(hello) def(greet, greet);Python 代码: import hello print hello.greet()hello, world利用普通方式对 C+进行 python 的嵌入,实现将一个二维数组传入 python 脚本,计算所有数据的和,并返回其结果。 (可以实现多维数组的传入,类似 dmatrix 矩阵)C+代码:#include #include #include int main(int argc, char *argv)QApplication a(argc,

10、argv);PyObject *modulename, *module, *dic, *func, *args, *list;char *funcname1 = sum;printf(-=在C中嵌入Python=-n );/* Python解释器的初始化*/Py_Initialize(); if(!Py_IsInitialized() printf(初始化失败! ); return -1; /* 导入Python模块,并检验是否正确导入*/modulename = Py_BuildValue(s, functionText);module = PyImport_Import(modulenam

11、e); if(!module) printf(导入pytest 失败! ); return -1; /* 获得模块中函数并检验其有效性 */dic = PyModule_GetDict(module);if(!dic)printf(错误!n);return -1; /* 获得sum函数地址并验证*/func = PyDict_GetItemString(dic,funcname1); if(!PyCallable_Check(func) printf(不能找到函数%s ,funcname1); return -1; printf(使用Python中的sum 函数求解二维数组所有元素之和n);/

12、* 构建两行四列的二维数组 1 2 3 45 6 7 8 */list = Py_BuildValue(iiii)(iiii),1,2,3,4,5,6,7,8);/* 构建sum函数的参数元组*/args = PyTuple_New(1);PyTuple_SetItem(args,0,list);/* 调用sum函数*/PyObject* rempResult = PyObject_CallObject(func,args);long rel = PyInt_AsLong(rempResult); /求和printf(调用sum函数返回值:%dn,rel);/* 释放资源*/Py_DECREF(list);Py_DECREF(args);Py_DECREF(module);/* 结束Python解释器*/Py_Finalize(); printf(按回车键退出程序:n);getchar(); return 0;return a.exec();Python代码:import numpy as npdef sum(p1): rows = len(p1)columns = len(p10)sum = 0for i in range(rows):for j in range(columns):sum+=p1ij return sum网上资料:http:/

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号