Python 扩展 : symbol(s) not found for architecture x86_64 error

标签 python c python-2.7

我想用 c 写一个 python 扩展。我在 Mac 上工作,我从 here 中获取了代码:

#include <Python.h>

static PyObject* say_hello(PyObject* self, PyObject* args)
{
    const char* name;

    if (!PyArg_ParseTuple(args, "s", &name))
        return NULL;

    printf("Hello %s!\n", name);

    Py_RETURN_NONE;
}

static PyMethodDef HelloMethods[] =
{
     {"say_hello", say_hello, METH_VARARGS, "Greet somebody."},
     {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC

inithello(void)
{
     (void) Py_InitModule("hello", HelloMethods);
}

我编译它:

gcc -c -o py_module.o py_module.c -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/
gcc -o py_module py_module.o -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/ -lm

但是我得到这个错误:

Undefined symbols for architecture x86_64:
  "_PyArg_ParseTuple", referenced from:
      _say_hello in py_module.o
  "_Py_InitModule4_64", referenced from:
      _inithello in py_module.o
  "__Py_NoneStruct", referenced from:
      _say_hello in py_module.o
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [py_module] Error 1

为什么python不支持X86_64架构?

最佳答案

两件事:

  • 您需要将您的扩展链接为一个共享对象(您正在尝试链接一个可执行文件,这就是链接器寻找 main() 的原因);
  • 您需要链接到 Python 静态库 (-lpython)。

关于Python 扩展 : symbol(s) not found for architecture x86_64 error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15631882/

相关文章:

python - 类没有指定表或表名,也没有从现有的表映射类继承

C 指向 Swift 指针的指针

python - 查找并替换 numpy ndarray 中的特定值?

python - 如何在 Python 中正确定义静态实用程序类

python - 将 pandas df 从长转换为宽,然后转换为稀疏矩阵

python - django.db.utils.OperationalError : FATAL: password authentication failed for user "postgres"

c - 为什么在正确的文件夹 "/lib"和 "/usr/lib"中链接库(如 pthread)?

c - 这个功能是如何工作的?

python - 无法弄清楚为什么代码在 Python 3 中工作,而不是 2.7

python - 令人费解的python索引错误