【问题标题】:Linker error: C/C++ Extensions for python链接器错误:python 的 C/C++ 扩展
【发布时间】:2014-09-03 01:43:41
【问题描述】:

正如我的标题所说,我正在尝试为 Python 开发一个 C 扩展。我在这里遵循了本教程并运行了 setup.py 脚本。但是,当我运行 python 解释器并尝试导入我新创建的模块时,我得到一个链接器错误未定义符号: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'])])

【问题讨论】:

    标签: python c++ c python-3.x


    【解决方案1】:

    这是因为函数被称为Py_BuildValue 而不是py_BuildValue。 C 区分大小写。如果您进一步检查编译消息,您可能还会在此处收到有关函数被隐式声明的警告。

    【讨论】:

    • 哦,嗯。我正在关注一个教程。事实上,科迪是复制和粘贴的。但我想这可能是它谢谢。
    • 我对此进行了跟进。众所周知,我会做这种愚蠢的事情,但看起来这不是问题。
    • @rady:你确定是同一个错误吗?当我尝试编译您的代码但更正 Py_BuildValue 大写时,错误会转移到关于 Py_InitModule3 的构建,这是可以理解的,因为这是 Python 2 函数,而您似乎正在尝试为 Python 3 编译。
    • 这是第二个错误。我将如何适应 python3
    猜你喜欢
    • 1970-01-01
    • 2022-08-18
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 2011-10-24
    • 1970-01-01
    相关资源
    最近更新 更多