我剛開始嘗試用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
從此評論看:868次,沒有投票。有沒有搞錯? – Scott