使用cython的外部库导致ImportError

时间:2017-05-27 17:09:08

标签: cython

我用cython导入了一些外部库:

cdef extern from "zmq.h" nogil:
int zmq_msg_init (zmq_msg_t *msg)
int zmq_msg_init_size (zmq_msg_t *msg, size_t size)
int zmq_bind (zmq_msg_t *msg, void *data,
    size_t size, zmq_free_fn *ffn, void *hint)  
...

并收到以下错误:

g++ -bundle -undefined dynamic_lookup -L/Users/taghawi/anaconda/envs/py3/lib -L/Users/taghawi/anaconda/envs/py3/lib -arch x86_64 build/temp.macosx-10.7-x86_64-3.6/processorgroup.o -L/Users/taghawi/anaconda/envs/py3/lib -lstdc++ -o /Users/taghawi/Dropbox/workspace/ABCEabsinth/processorgroup.cpython-36m-darwin.so
Traceback (most recent call last):
  File "start.py", line 1, in <module>
    from processorgroup import ProcessorGroup
ImportError: 
dlopen(/Users/taghawi/Dropbox/workspace/ABCEabsinth/processorgroup.cpython-36m-darwin.so, 2): Symbol not found: _zmq_bind
  Referenced from: /Users/taghawi/Dropbox/workspace/ABCEabsinth/processorgroup.cpython-36m-darwin.so
  Expected in: flat namespace
 in /Users/taghawi/Dropbox/workspace/ABCEabsinth/processorgroup.cpython-36m-darwin.so

1 个答案:

答案 0 :(得分:1)

问题是,如果使用外部库&#34; zmq.h&#34;在这种情况下,需要在编译中指定:

setup(
  name = 'cagent',
  ext_modules=cythonize([
 Extension('processorgroup', ['processorgroup.pyx'],
             language="c++",
             libraries=["stdc++", "zmq"],
             include_dirs=[numpy.get_include()],
            ),
          ]),
 cmdclass = {'build_ext': build_ext}
)

注意&#34; zmq&#34; in&#39; libraries = [&#34; stdc ++&#34;,&#34; zmq&#34;],&#39;