extending python with c
2017-10-10 12:38
253 查看
c_fib.c
#include <Python.h> int Cfib(int n) { if (n < 2) { return n; } else { return Cfib(n-1) + Cfib(n-2); } } static PyObject* fib(PyObject* self, PyObject* args) { int n; if (!PyArg_ParseTuple(args, "i", &n)) { return NULL; } return Py_BuildValue("i", Cfib(n)); } static PyObject* version(PyObject* self) { return Py_BuildValue("s", "Version 1.0"); } static PyMethodDef myMethods[] = { {"fib", fib, METH_VARARGS, "Calculates the Fibonacci number."}, {"version", (PyCFunction)version, METH_NOARGS, "Returns the version."}, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC initc_fib(void) { (void)Py_InitModule("c_fib", myMethods); }
setup.py
from distutils.core import setup, Extension module = Extension('myModule', sources = ['myModule.c']) setup(name='PackageName', version='1.0', description='This is a package for myModule', ext_modules=[module])
install mingw gcc.exe
python setup.py build
相关文章推荐
- Extending Python with C or C++
- Extending Python with C or C++
- Embeding Python & Extending Python with FFPython
- 用C/C++扩展Python(Extending Python with C or C++)
- 编写Python扩展(Extending Python with C or C++)
- Extending Python with C\C++ 实践问题
- 用C/C++扩展Python(Extending Python with C or C++)
- Extending Python with C or C++¶
- python extending with c++
- Introduction.to.Machine.Learning.with.Python 笔记
- Web Scraping with Python-Chapter1读书笔记
- 理解Python的With语句
- LeetCode 30 Substring with Concatenation of All Words (C,C++,Java,Python)
- Programming Computer Vision with Python (学习笔记十二)
- python with..as
- 机器学习: Python with Recurrent Neural Network
- OpenCV学习笔记(六十二)——《OpenCV Computer Version with Python》阅读摘要
- [debug] debugging in python with pdb
- python的with用法
- Compiling VIM with python support