gpt4 book ai didi

python - Cython、OS X 和链接错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:35:44 26 4
gpt4 key购买 nike

我按照 Cython 的简单教程执行了以下步骤:

  1. 创建一个简单的 python 文件 testme.py:

    print(“你好!!”)

  2. 使用 cython 创建一个 c 文件:

    cython -a testme.py

  3. 使用 gcc 编译生成的 testme.c 文件:

    gcc -Os -I/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/include/python2.7 -o testme testme.c -lpthread -lm -lutil -ldl

结果是很多这样的行:

Undefined symbols for architecture x86_64:
"_PyCode_New", referenced from:
_inittestme in testme-4ef125.o
"_PyDict_New", referenced from:
_inittestme in testme-4ef125.o
"_PyDict_SetItem", referenced from:
_inittestme in testme-4ef125.o
"_PyErr_Clear", referenced from:
_inittestme in testme-4ef125.o
_main in testme-4ef125.o
"_PyErr_Occurred", referenced from:
_inittestme in testme-4ef125.o
_main in testme-4ef125.o
"_PyErr_Print", referenced from:
_main in testme-4ef125.o
"_PyErr_SetString", referenced from:
_inittestme in testme-4ef125.o
"_PyErr_WarnEx", referenced from:
_inittestme in testme-4ef125.o
"_PyExc_ImportError", referenced from:
_inittestme in testme-4ef125.o
"_PyExc_RuntimeError", referenced from:
_inittestme in testme-4ef125.o

显然,缺少链接库。我有以下内容

/usr/lib/libpython2.6.dylib -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/Python

但是将其添加到 gcc 命令中会出现 XXX not found 错误:

gcc -Os -I /usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/include/python2.7 -o hello testme.c -l/usr/lib/libpython2.6.dylib -lpthread -lm -lutil -ldl
ld: library not found for -l/usr/lib/libpython2.6.dylib

路径正确。图书馆就在那里。

有什么提示吗?

最佳答案

您需要链接正确的 libpython。例如。 -lpython2.7.

FWIW cython 命令有点低级(它处理将单个 Cython 模块编译为 C 的编译),其中 cythonize 命令的级别更高一些,可以在整个包上工作,并且还包含一个用于编译 C 代码的 --build 选项。这通常比尝试自己构建正确的 gcc 命令更容易。

关于python - Cython、OS X 和链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46964232/

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