【问题标题】:Cython, OS X and link errorsCython、OS X 和链接错误
【发布时间】:2017-10-26 21:37:24
【问题描述】:

我已经按照以下步骤完成了关于 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

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

有什么提示吗?

【问题讨论】:

    标签: python gcc cython


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!使用正确的库就可以了。有一件事,我没有看到 cython 的 --build 选项。你指的是别的东西吗?
    • 很高兴它成功了。 --build 选项实际上是 cythonize 的一部分。我会更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2016-10-30
    • 2017-06-11
    • 1970-01-01
    相关资源
    最近更新 更多