Cython,OS X和链接错误

时间:2017-10-26 21:37:24

标签: python gcc cython

我已经按照以下步骤对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

  4. 结果是这样的几行:

    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
    

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

    任何提示?

1 个答案:

答案 0 :(得分:1)

您需要链接正确的libpython。例如。 redis-stable

FWIW ./src/redis-cli命令有点低级(处理将各个Cython模块编译为C),其中-lpython2.7命令稍微高一点level,可以在整个包上工作,还包括一个cython选项,用于处理编译C代码。这通常比尝试自己构建正确的cythonize命令更容易。