zoukankan      html  css  js  c++  java
  • extending python with c

    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

  • 相关阅读:
    MySQL(一)
    HTML基础
    python函数基础
    常用的模块
    面向对象进阶
    定制自己的数据类型
    Shell篇之AWK
    MATLAB如何实现傅里叶变换FFT?有何物理意义?
    傅里叶分析
    2018年度关键词
  • 原文地址:https://www.cnblogs.com/otfsenter/p/7645007.html
Copyright © 2011-2022 走看看