python之C扩展二
来源:互联网 发布:linux 查看文件命令 编辑:程序博客网 时间:2024/06/05 04:58
1.c代码
/************************************************************************* > File Name: python_test.c > Author: > Mail: > Created Time: Thu 03 Dec 2015 10:45:04 PM PST ************************************************************************/#include <python2.7/Python.h> //包含python的头文件 /*1 c/cpp中的函数 */int my_c_function(const char *arg) { int n = system(arg); return n; } // 2 python 包装 static PyObject * wrap_my_c_fun(PyObject *self, PyObject *args) { const char * command; int n; if (!PyArg_ParseTuple(args, "s",&command))//这句是把python的变量args转换成c的变量command return NULL; n = my_c_function(command);//调用c的函数 return Py_BuildValue("i",n);//把c的返回值n转换成python的对象 } // 3 方法列表 static PyMethodDef MyCppMethods[] = { //MyCppFun1是python中注册的函数名,wrap_my_c_fun是函数指针 { "MyCppFun1", wrap_my_c_fun, METH_VARARGS, "Execute a shellcommand." }, { NULL, NULL, 0, NULL } }; // 4 模块初始化方法 PyMODINIT_FUNC initMyCppModule(void) { //初始模块,把MyCppMethods初始到MyCppModule中 PyObject *m = Py_InitModule("MyCppModule", MyCppMethods); if (m == NULL) return; }
2.python代码 setup.py
#!/usr/bin env pythonfrom distutils.core import setup, ExtensionMOD = 'MyCppModule'setup(name=MOD,version = '1.0', ext_modules=[Extension(MOD,sources=['python_test.c'])])
3.编译
kjlr@kjlr-virtual-machine:~/python_test$ python setup.py build
running buildrunning build_extbuilding 'MyCppModule' extensionx86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c python_test.c -o build/temp.linux-x86_64-2.7/python_test.ocreating build/lib.linux-x86_64-2.7x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/python_test.o -o build/lib.linux-x86_64-2.7/MyCppModule.so
4.连接至python
kjlr@kjlr-virtual-machine:~/python_test$ sudo python setup.py install
running installrunning buildrunning build_extrunning install_libcopying build/lib.linux-x86_64-2.7/MyCppModule.so -> /usr/local/lib/python2.7/dist-packagesrunning install_egg_infoWriting /usr/local/lib/python2.7/dist-packages/MyCppModule-1.0.egg-info
5.测试代码 test1.py
#!/usr/bin/env pythonimport MyCppModulea = "ls"MyCppModule.MyCppFun1(a)
测试结果(python运行ls命令):kjlr@kjlr-virtual-machine:~/python_test$ python test1.py build python_test.c setup.py test1.py
0 0
- python之C扩展二
- python之C扩展
- 使用C语言扩展Python(二)
- 用c扩展python
- C扩展python实例
- C语言扩展Python
- C语言扩展Python
- python c扩展
- Python的C扩展
- Python:C语言扩展
- Python 扩展C
- python C/C++ 扩展
- python扩展C
- GNU C 扩展(二)
- GNU C 扩展(二)
- (Python编程)C扩展概览
- 用C/C++扩展Python
- 使用C/C++扩展Python
- 【Android游戏开发之十】(优化处理)详细剖析Android Traceview效率检视工具,分析程序运行速度!并讲解两种创建SDcard方式!
- 关于自定义控件的自定义属性
- java泛型程序设计——无限定通配符+通配符捕获
- Linux下静态库的生成以及使用实例图解
- 最大矩形
- python之C扩展二
- java 中string filename和file filename 创建文件的区别
- 高斯函数以及在图像处理中的应用总结
- CEdit & CRichEdit 使用技巧
- osgOcean笔记
- ViewHolder高效使用
- 基于PHP的cURL使用入门
- leetcode -- Majority Element -- 简单,但是还有很多其他方法
- String.Format 大全