Import error undefined symbol (C++ module in pytho

2019-07-22 20:30发布

问题:

I know there are lots of similar questions on the website but I couldn't find an answer to my problem.

I'm wrapping C++ classes with Cython in order to use them with Python3. After building the external module with a setup.py, when I run the python program I got the following error:

from "name of.pyx file" import "name of the class to import" Import error: /home/.../filename.so: undefined symbol: _ZTINSt8ios_base7failureB5cxx11E.

I'm on Ubuntu 16.04, I build the extensions from the terminal with the command line python3 setup.py build_ext --inplace, and then run the .py from the terminal or from Spyder in Anaconda (I got the error in both cases.)

From what I read the error might come from the cython compilation because i'm not linking some libraries. Is this true? If it is, could someone explain me how to do it?

I let you here my setup.py, in comments all the different setups I tried.

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()]
)

回答1:

The problem was solved after installing libgcc in anaconda: conda install libgcc, there was a missing library.