3

I tried to write a Cython wrapper around the C++ library http://primesieve.org/

It wraps a single function count. So far, it installs correctly python setup.py install, but when I import primesieve the function primesieve.count is missing. Any ideas?


primesieve.pxd (following 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"])])
)
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465

1 Answers1

2

Modify setup.py to link against libprimesieve.so by adding libraries = ["primesieve"] to you arguments to the Extension constructor. Without it, you'll get this error:

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

Once I changed setup.py, it worked for me:

$ python2 setup.py build
...
$ (cd build/lib.linux-x86_64-2.7 && python2 -c 'import primesieve; print primesieve.count(None, 5)')
3
Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
  • Aha, thanks. Where is the documentation for `setuptools.Extension`? – Colonel Panic Jul 04 '15 at 09:14
  • Cool I also fixed an error `/usr/bin/ld.bfd.real: cannot find -lprimesieve` by installing the library `./configure --prefix=$HOME && make && make install` and adding `library_dirs = ["/home/colonelpanic/lib"]` to Extension in setup.py . Is that the right way to do it? – Colonel Panic Jul 04 '15 at 09:20
  • Doesn't feel right to put absolute path to my home folder in there – Colonel Panic Jul 04 '15 at 09:26
  • Opened a new question https://stackoverflow.com/questions/31220762/how-to-build-library-without-sudo – Colonel Panic Jul 04 '15 at 12:48