我的Cython出了什么问题?

时间:2015-07-03 23:49:02

标签: cython

我试图围绕C ++库http://primesieve.org/

编写一个Cython包装器

它包含一个函数count。到目前为止,它安装正确python setup.py install,但当我import primesieve时,函数primesieve.count丢失了。有什么想法吗?

primesieve.pxd(关注http://docs.cython.org/src/tutorial/clibraries.html

cdef extern from "stdint.h":
    ctypedef unsigned long long uint64_t

cdef extern from "primesieve/include/primesieve.h":
    uint64_t primesieve_count_primes(uint64_t start, uint64_t stop)

primesieve.pyx

cimport primesieve

cpdef int count(self, int n):
    return primesieve.primesieve_count_primes(1, n)

setup.py

from setuptools import setup, Extension
from Cython.Build import cythonize

setup(
    ext_modules = cythonize([Extension("*", ["primesieve.pyx"], include_dirs = ["primesieve/include"])])
)

1 个答案:

答案 0 :(得分:2)

通过向setup.py构造函数的参数添加libprimesieve.so,修改libraries = ["primesieve"]以链接Extension。没有它,你会收到这个错误:

ImportError: ./primesieve.so: undefined symbol: primesieve_count_primes

我改变了setup.py后,对我有用了:

$ python2 setup.py build
...
$ (cd build/lib.linux-x86_64-2.7 && python2 -c 'import primesieve; print primesieve.count(None, 5)')
3