【发布时间】:2017-02-21 10:02:13
【问题描述】:
我知道网站上有很多类似的问题,但我找不到我的问题的答案。
我正在用 Cython 包装 C++ 类,以便在 Python3 中使用它们。使用setup.py 构建外部模块后,当我运行 python 程序时,出现以下错误:
from "name of.pyx file" import "name of the class to import"
Import error: /home/.../filename.so: undefined symbol: _ZTINSt8ios_base7failureB5cxx11E.
我在 Ubuntu 16.04 上,我使用命令行 python3 setup.py build_ext --inplace 从终端构建扩展,然后从终端或 Anaconda 中的 Spyder 运行 .py(在这两种情况下我都遇到了错误。)
从我读到的错误可能来自 cython 编译,因为我没有链接某些库。这是真的?如果是的话,有人可以解释我该怎么做吗?
我让你在这里我的setup.py,在 cmets 中我尝试了所有不同的设置。
setup.py
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
#setup(ext_modules = cythonize(
#"pycoralv1.pyx", # our Cython source
#sources=["coralv1cpp.cpp"], # additional source file(s)
#language="c++", # generate C++ code
#))
#setup(ext_modules = cythonize(Extension(
# "pyCoralv1", # the extension name
# sources=["pyCoralv1.pyx", "Coralv1cpp.cpp"], # the Cython source and
# additional C++ source files
# language="c++", # generate and compile C++ code
# )))
#setup(
# name = "testcoral",
# ext_modules = cythonize('*.pyx'),
#)
ext_modules = [
Extension(
"pyCoralv1",
sources=["pyCoralv1.pyx", "Coralv1cpp.cpp"],
extra_compile_args=['-fopenmp',"-fPIC"],
extra_link_args=['-fopenmp',"-I", "/usr/include/glib-2.0", "-l", "glib-2.0", "-I", "/usr/lib/x86_64-linux-gnu/glib-2.0/include"],
language="c++",
)
]
for e in ext_modules:
e.pyrex_directives = {"boundscheck": False}
setup(
name='Coral library',
ext_modules=cythonize(ext_modules),
include_dirs = [numpy.get_include()]
)
【问题讨论】:
-
这取决于未定义的符号。您可以使用
librariesandlibrary_dirsas arguments to Extension to specify what to link to。但是有很多可能是得到未定义的符号,所以现在无法回答。 -
谢谢您的回答。问题是符号名称是由Cython创建的,所以我无法识别问题的根源...
标签: python-3.x cython cythonize