2016-08-18 118 views
2

我嘗試安裝我的功能,用C語言編寫(與python3 setup.py的安裝),但蟒蛇提高導入錯誤:動態模塊沒有定義模塊輸出功能(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蟒蛇3.5.2

編輯: python3-dev的版本是3.5.1-3

回答

1

最後我用了一個骯髒的把戲!

編譯的C代碼(沒有python.h和在任何C蟒數據類型)其中:

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

和使用蟒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] 
相關問題