C扩展python实例
来源:互联网 发布:最好的足球数据网站 编辑:程序博客网 时间:2024/05/16 10:09
mm@R1-A007 ~/gordon_space/ctop $ cat example.c
//example.c
int fact(int n){
if(n<=1)
return 1;
else
return n*fact(n-1);
}
mm@R1-A007 ~/gordon_space/ctop $ cat wrap.c
//filename: wrap.c
#include<Python.h>
PyObject* warp_fact(PyObject* self, PyObject* args){
int n, result;
if (! PyArg_ParseTuple(args,"i:fact",&n))
return NULL;
result = fact(n);
return Py_BuildValue("i",result);
}
static PyMethodDef exampleMethods[] = {
{"fact",warp_fact,METH_VARARGS,"Caculate N!"}, // fact为模块名字,第三个参数为传值方式,最后一个为注释
{NULL,NULL} //估计是结束符吧
};
void initexample(){
PyObject* m;
m = Py_InitModule("example", exampleMethods);
}
mm@R1-A007 ~/gordon_space/ctop $ gcc -fpic -c -I /usr/include/python2.7 -I /usr/lib/python2.7/config example.c wrap.c
mm@R1-A007 ~/gordon_space/ctop $ ls
example.c example.o wrap.c wrap.o
mm@R1-A007 ~/gordon_space/ctop $ gcc -shared -o example.so example.o wrap.o
mm@R1-A007 ~/gordon_space/ctop $ ls
example.c example.o example.so wrap.c wrap.o
mm@R1-A007 ~/gordon_space/ctop $ python2.7
Python 2.7.1 (r271:86832, Sep 7 2011, 12:55:30)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> dir(example)
['__doc__', '__file__', '__name__', '__package__', 'fact']
>>> example.fact(6)
//example.c
int fact(int n){
if(n<=1)
return 1;
else
return n*fact(n-1);
}
mm@R1-A007 ~/gordon_space/ctop $ cat wrap.c
//filename: wrap.c
#include<Python.h>
PyObject* warp_fact(PyObject* self, PyObject* args){
int n, result;
if (! PyArg_ParseTuple(args,"i:fact",&n))
return NULL;
result = fact(n);
return Py_BuildValue("i",result);
}
static PyMethodDef exampleMethods[] = {
{"fact",warp_fact,METH_VARARGS,"Caculate N!"}, // fact为模块名字,第三个参数为传值方式,最后一个为注释
{NULL,NULL} //估计是结束符吧
};
void initexample(){
PyObject* m;
m = Py_InitModule("example", exampleMethods);
}
mm@R1-A007 ~/gordon_space/ctop $ gcc -fpic -c -I /usr/include/python2.7 -I /usr/lib/python2.7/config example.c wrap.c
mm@R1-A007 ~/gordon_space/ctop $ ls
example.c example.o wrap.c wrap.o
mm@R1-A007 ~/gordon_space/ctop $ gcc -shared -o example.so example.o wrap.o
mm@R1-A007 ~/gordon_space/ctop $ ls
example.c example.o example.so wrap.c wrap.o
mm@R1-A007 ~/gordon_space/ctop $ python2.7
Python 2.7.1 (r271:86832, Sep 7 2011, 12:55:30)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> dir(example)
['__doc__', '__file__', '__name__', '__package__', 'fact']
>>> example.fact(6)
720
- C扩展python实例
- 用C语言扩展Python的功能的实例
- 用C语言扩展Python的功能的实例
- 用c扩展python
- C语言扩展Python
- C语言扩展Python
- python c扩展
- Python的C扩展
- Python:C语言扩展
- Python 扩展C
- python之C扩展
- python C/C++ 扩展
- python扩展C
- (Python编程)C扩展概览
- 用C/C++扩展Python
- 使用C/C++扩展Python
- Python 的C语言扩展
- python的C、c++扩展
- 不同收入水平职工家庭 申请公积金贷款情况
- 查询数据库阻塞锁的SQL
- 理解RESTful架构-----认识一下RESTful架构到底是回事?
- Flash开发iOS应用全攻略(四)——如何为iTunes Connect准备应用
- 10大协作办公工具:实现团队工作效率最大化
- C扩展python实例
- hover
- 指向类成员的指针并非指针
- HubbleDotNet开源全文搜索数据库项目--技术详解
- Flash开发iOS应用全攻略(五)——如何上传应用到iTunes Connect
- mvc学习总结
- 对STM32系统时钟和分频的一点理解
- 怎样在Android中解析doc、docx、xls、xlsx格式文件?
- hdu 2688