gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:21:28 25 4
gpt4 key购买 nike

我想用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com