I am trying to write a C extension for python. With the code (below), I get a compiler warning:
implicit declaration of function 'Py_InitModule'
And it does not work at runtime with this error:
undefined symbol: Py_InitModule
I spent literally hours finding a solution without joy. I tried a few minor syntax changes, I even found a message stating that the method is deprecated. However, I do not find a replacement.
Here is the code:
#include <Python.h> //a func to calc fib numbers int cFib(int n) { if (n<2) return n; 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 PyMethodDef module_methods[] = { {"fib",(PyCFunction) fib, METH_VARARGS,"calculates the fibonachi number"}, {NULL,NULL,0,NULL} }; PyMODINIT_FUNC initcModPyDem(void) { Py_InitModule("cModPyDem",module_methods,"a module"); }
If this helps, this is my setup.py:
from distutils.core import setup, Extension module = Extension('cModPyDem', sources=['cModPyDem.c']) setup(name = 'packagename', version='1.0', description = 'a test package', ext_modules = [module])
And the test code in test.py:
import cModPyDem if __name__ == '__main__' : print(cModPyDem.fib(200))
Any help would be very, very much appreciated.
c python python-extensions
Sam redway
source share