正如我的标题所说,我正在尝试为 Python 开发 C 扩展。我在这里按照本教程进行操作并运行了 setup.py 脚本。当我运行 python 解释器并尝试导入我新创建的模块时,我得到一个链接器错误 undefined symbol :py_BuildValue。我也尝试自己编译它,但我得到了同样的错误加上一个错误,说 Py_InitModule3 是未定义的。我已经安装了 python3.2-dev 和 python3-dev。这是我的 test.c 代码:
#include <python3.2/Python.h>
static PyObject* Test(PyObject* self){
return py_BuildValue("s","This is a test and my first trip into the world of python bindings!!");
}
static PyMethodDef test_funcs[] ={{"testExtensions",(PyCFunction)Test, METH_NOARGS,"This is my First Extension!!"},{NULL}};
void initTest(void){
Py_InitModule3("Test", test_funcs, "Extension module example!");
}
还有我的 setup.py 代码:
from distutils.core import setup, Extension
setup(name='Test', version='1.0', \
ext_modules=[Extension('Test', ['test.c'])])
最佳答案
那是因为该函数被称为 Py_BuildValue
而不是 py_BuildValue
。 C 区分大小写。如果您进一步检查编译消息,您可能还会收到有关函数被隐式声明的警告。
关于python - 链接器错误 : C/C++ Extensions for python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25634975/