ImportError: dynamic module does not define init function
10,938
Because the function initextPy
is a C++ function which causes the C++ compiler to mangle the name so it's not recognizable.
You need to mark the function as extern "C"
to inhibit the name mangling:
extern "C" void initextPy(void)
{
...
}
Related videos on Youtube
Author by
P Alcove
Updated on June 28, 2022Comments
-
P Alcove almost 2 years
I am trying to reproduce the following tutorial https://csl.name/post/c-functions-python/.
My Python extension in C++ looks like:
#include <Python.h> static PyObject* py_myFunction(PyObject* self, PyObject* args) { char *s = "Hello from C!"; return Py_BuildValue("s", s); } static PyObject* py_myOtherFunction(PyObject* self, PyObject* args) { double x, y; PyArg_ParseTuple(args, "dd", &x, &y); return Py_BuildValue("d", x*y); } static PyMethodDef extPy_methods[] = { {"myFunction", py_myFunction, METH_VARARGS}, {"myOtherFunction", py_myOtherFunction, METH_VARARGS}, {NULL, NULL} }; void initextPy(void) { (void) Py_InitModule("extPy", extPy_methods); }
I am using the following setup.py:
from distutils.core import setup, Extension setup(name='extPy', version='1.0', \ ext_modules=[Extension('extPy', ['extPy.cpp'])])
After invoking it with
python setup.py install
the .so file seems to be in the right place.But when I try to use this extension with
import extPy
I get the Error:ImportError: dynamic module does not define init function
What am I missing here? Thanks for Help.
-
P Alcove over 9 yearsThis did the job. Thank you.