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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步