2017-04-25 222 views
1

我剛開始嘗試用C來寫我的第一個Python擴展模塊,並使用在https://www.tutorialspoint.com/python/python_further_extensions.htmC++ Python模塊導入錯誤:「未定義的符號:Py_InitModule3」(Py_InitModule())

提供的說明,我在Linux上Mint 18.1,在其virtualenv版本中使用Python 3.6.1。

作爲第一步,我編譯了一個我打算編寫的Python模塊的極簡版本。

這裏我的C代碼:

include <Python.h> 

static PyObject* uniqueCombinations(PyObject* self) 
{ 
    return Py_BuildValue("s", "uniqueCombinations() return value (is of type 'string')"); 
} 

static char uniqueCombinations_docs[] = 
    "usage: uniqueCombinations(lstSortableItems, comboSize)\n"; 

static PyMethodDef uniqueCombinations_funcs[] = { 
    {"uniqueCombinations", (PyCFunction)uniqueCombinations, 
    METH_NOARGS, uniqueCombinations_docs}, 
    {NULL} 
}; 

void inituniqueCombinations(void) 
{ 
    Py_InitModule3("uniqueCombinations", uniqueCombinations_funcs, 
        "Extension module uniqueCombinations v. 0.01"); 
} 

,在這裏我都用來編譯C代碼的setup.py文件的代碼uniqueCombinations.cpython-36m-x86_64-linux-gnu.so

from distutils.core import setup, Extension 

setup(name='uniqueCombinations', version='0.01', \ 
     ext_modules=[Extension('uniqueCombinations', ['uniqueCombinations_pythonCmodule_v-0.01_Cg.c'])]) 

令我驚訝的

>>> import uniqueCombinations 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
ImportError: uniqueCombinations.cpython-36m-x86_64-linux-gnu.so: undefined symbol: Py_InitModule3 

Why am I getting this error?

And what should I do to be able to load the compiled module properly?

:模塊沒有進口正確加載

這裏構建協議與約Py_InitModule3的隱式聲明警告:

$ python uniqueCombinations_pythonCmodule_v-0.01_setup.py_Cg.py build 
running build 
running build_ext 
building 'uniqueCombinations' extension 
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python3.6m -c uniqueCombinations_pythonCmodule_v-0.01_Cg.c -o build/temp.linux-x86_64-3.6/uniqueCombinations_pythonCmodule_v-0.01_Cg.o 
uniqueCombinations_pythonCmodule_v-0.01_Cg.c: In function ‘inituniqueCombinations’: 
uniqueCombinations_pythonCmodule_v-0.01_Cg.c:19:5: warning: implicit declaration of function ‘Py_InitModule3’ [-Wimplicit-function-declaration] 
    Py_InitModule3("uniqueCombinations", uniqueCombinations_funcs, 
    ^
creating build/lib.linux-x86_64-3.6 
gcc -pthread -shared build/temp.linux-x86_64-3.6/uniqueCombinations_pythonCmodule_v-0.01_Cg.o -o build/lib.linux-x86_64-3.6/uniqueCombinations.cpython-36m-x86_64-linux-gnu.so 
+0

從此評論看:868次,沒有投票。有沒有搞錯? – Scott

回答

2

的Py_InitModule(Py_InitModule3)不再使用。有必要創建一個PyModuleDef結構,然後將其引用傳遞給PyModule_Create。

下面導入時從中創建的模塊不會引發錯誤的C代碼:

#include <Python.h> 

static PyObject* uniqueCombinations(PyObject* self) 
{ 
    return Py_BuildValue("s", "uniqueCombinations() return value (is of type 'string')"); 
} 

static char uniqueCombinations_docs[] = 
    "usage: uniqueCombinations(lstSortableItems, comboSize)\n"; 

/* deprecated: 
static PyMethodDef uniqueCombinations_funcs[] = { 
    {"uniqueCombinations", (PyCFunction)uniqueCombinations, 
    METH_NOARGS, uniqueCombinations_docs}, 
    {NULL} 
}; 
use instead of the above: */ 

static PyMethodDef module_methods[] = { 
    {"uniqueCombinations", (PyCFunction) uniqueCombinations, 
    METH_NOARGS, uniqueCombinations_docs}, 
    {NULL} 
}; 


/* deprecated : 
PyMODINIT_FUNC init_uniqueCombinations(void) 
{ 
    Py_InitModule3("uniqueCombinations", uniqueCombinations_funcs, 
        "Extension module uniqueCombinations v. 0.01"); 
} 
*/ 

static struct PyModuleDef Combinations = 
{ 
    PyModuleDef_HEAD_INIT, 
    "Combinations", /* name of module */ 
    "usage: Combinations.uniqueCombinations(lstSortableItems, comboSize)\n", /* module documentation, may be NULL */ 
    -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ 
    module_methods 
}; 

PyMODINIT_FUNC PyInit_Combinations(void) 
{ 
    return PyModule_Create(&Combinations); 
} 

這裏構建協議:

$ python uniqueCombinations_pythonCmodule_v-0.01_setup.py_Cg.py build 
running build 
running build_ext 
building 'Combinations' extension 
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python3.6m -c uniqueCombinations_pythonCmodule_v-0.01_Cg.c -o build/temp.linux-x86_64-3.6/uniqueCombinations_pythonCmodule_v-0.01_Cg.o 
gcc -pthread -shared build/temp.linux-x86_64-3.6/uniqueCombinations_pythonCmodule_v-0.01_Cg.o -o build/lib.linux-x86_64-3.6/Combinations.cpython-36m-x86_64-linux-gnu.so 

,併成功導入了交互式Python控制檯會話:

>>> import Combinations 
>>> dir(Combinations) 
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'uniqueCombinations'] 
>>> Combinations.__doc__ 
'usage: Combinations.uniqueCombinations(lstSortableItems, comboSize)\n' 
>>> Combinations.uniqueCombinations() 
"uniqueCombinations() return value (is of type 'string')" 
>>> 
相關問題