python: ImportError: динамический модуль не определяет функцию экспорта модуля

Я пытаюсь установить свою функцию, написанную на c (с помощью python3 setup.py install), но python вызывает ImportError: динамический модуль не определяет функцию экспорта модуля (PyInit_costFunction) ошибка!

costFunction.c:

static PyObject *costFunction(PyObject *self, PyObject *args)
{
    return Py_BuildValue("d", 0); // or anything!
}

static PyMethodDef costFunction_methods[] = {
    {"costFunction", (PyCFunction)costFunction, METH_VARARGS, "cost function"},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef costFunctionmodule = {
    PyModuleDef_HEAD_INIT,"costFunction", NULL, -1, costFunction_methods
};

PyMODINIT_FUNC PyInit_costFunction(void)
{
    return PyModule_Create(&costFunctionmodule);
}

setup.py:

from distutils.core import setup, Extension
setup(name='costFunction', version='1.0',  \
      ext_modules=[Extension('costFunction', ['costFunction.c'],include_dirs=['include'])])

внешняя библиотека: tinyexpr

я использую linux mint 18 с python 3.5.2

РЕДАКТИРОВАТЬ: версия python3-dev - 3.5.1-3.


person Alireza Afzal aghaei    schedule 18.08.2016    source источник


Ответы (1)


наконец-то я прибегнул к подвоху!

скомпилированный код c (без python.h и любого типа данных python в C) с:

gcc -fPIC -Wall -O3 costFunction.c -o costFunction.so -shared  -fopenmp

и использовал модуль python ctypes для его загрузки и использования:

dll = ctypes.CDLL("./costFunction.so")
costFunction = dll.cost_function
costFunction.restype = ctypes.c_double
costFunction.argtypes = [ctypes.POINTER(ctypes.c_double), ctypes.c_int]
person Alireza Afzal aghaei    schedule 20.08.2016