C++对python模块进行扩展
2016-05-17 00:00
246 查看
摘要: 与C开发有点区别.
1) 使用的是C++
2) 代码
编译为so文件
g++ -fpic -c -I/usr/include/python2.7 -I /usr/lib/python2.7/config example.cpp
g++ -shared -o example.so example.o
1) 使用的是C++
2) 代码
#include <Python.h> #include <stdio.h> #include <iostream> #ifdef _WIN32 #include "stdafx.h" #include <Windows.h> #else #include <sys/stat.h> #include <time.h> #ifndef _MAX_PATH #define _MAX_PATH 255 #endif #endif using namespace std; static PyObject* wrap_decode_image(PyObject* self, PyObject* args) { // 2个参数 // 都是 Unicode 的字符串 char *xml_filepathname; char *image_filepathname; if (! PyArg_ParseTuple(args, "ss", &xml_filepathname,&image_filepathname)) return NULL; printf("input xml path: %s \r\n", xml_filepathname); printf("input image path: %s \r\n", image_filepathname); string xml(xml_filepathname); string image(image_filepathname); return Py_BuildValue("s", xml_filepathname); } static PyMethodDef pyDiscernMethods[] = { {"decode_image", wrap_decode_image, METH_VARARGS,"decode code"}, {NULL, NULL} }; //注意 C++ 需要使用 PyMODINIT_FUNC // 否则 g++ 编译成功,import之后提示找不到initpydiscern函数 PyMODINIT_FUNC initpydiscern(){ Py_InitModule("pydiscern", pyDiscernMethods); }
编译为so文件
g++ -fpic -c -I/usr/include/python2.7 -I /usr/lib/python2.7/config example.cpp
g++ -shared -o example.so example.o
相关文章推荐
- JAVA内部类实现多重继承父类[实现类似C++的多重继承功能]
- 在VS2013中使用boost库遇到的问题及解决
- 挖一下那些年的东东(模拟内存申请与释放C语言)
- C++中 关于结构体中有char *类型的指针 赋值问题
- POJ 8780 拦截导弹
- 【Endian】C语言的大小端及其简单的判断。
- C 和 C++ 一些基础
- c语言学习笔记8之if语句
- C语言字符输出格式化
- C++相关文章
- C++常见错误中英文对照表
- 逆元的使用
- C++随笔:.NET CoreCLR之GC探索(2)
- C++ STL--stack/queue 的使用方法
- C++实现二分查找
- Java和C++你知道这些吗?
- 飞机游戏、俄罗斯方块、贪吃蛇C语言代码
- vc++6.0 编译执行opencv1.0
- [C++Primer] 第二章 变量和基本类型
- C++对C的扩展之cin与cout