问题描述
在Cython中使用mlpack时遇到了未定义符号"问题.这是我的测试用例:
I met the problem "undefined symbol" when using mlpack in Cython. Here is my test case:
cdef extern from "<mlpack/core.hpp>" namespace "arma":
ctypedef unsigned uword
cdef cppclass vec:
vec()
vec(uword)
cdef cppclass mat:
mat()
mat(uword, uword)
void matprint "print" ()
double& operator() (const uword, const uword)
cdef extern from "<mlpack/methods/pca/pca.hpp>" namespace "mlpack::pca":
cdef cppclass ExactSVDPolicy:
ExactSVDPolicy()
cdef cppclass PCA[ExactSVDPolicy]:
PCA()
void Apply(const mat&, mat&, vec&, mat&)
cdef mat m = mat(4, 2)
(<double*>&m(0, 0))[0] = 1.2
(<double*>&m(1, 0))[0] = 1.0
(<double*>&m(2, 0))[0] = 0.8
(<double*>&m(3, 0))[0] = 0.6
(<double*>&m(0, 1))[0] = 0.6
(<double*>&m(1, 1))[0] = 0.8
(<double*>&m(2, 1))[0] = 1.0
(<double*>&m(3, 1))[0] = 1.2
cdef vec eig = vec(2)
cdef mat coeff = mat(4, 2)
cdef PCA[ExactSVDPolicy] pca
m.matprint()
pca.Apply(m, m, eig, coeff)
m.matprint()
这是设置文件:
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
setup(ext_modules = cythonize([Extension("pca", ["pca.pyx"], language='c++')]))
编译还可以,但是当我导入模块时,python抱怨:
Compilation was OK, but when I import the module, python complains that:
undefined symbol: _ZN6mlpack5Timer5StartERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
我正在寻找符号,它在libmlpack.so中定义.我将其放在/usr/local/lib中,该文件包含在LD_LIBRARY_PATH中,但似乎Python在运行时找不到该符号.有谁可以帮忙吗?谢谢.
I looked for the symbol, it is defined in the libmlpack.so. I put it in /usr/local/lib, which is included in LD_LIBRARY_PATH, but it seems Python does not find the symbol during runtime. Is there anyone who can help? Thanks.
推荐答案
扩展名必须链接到它正在使用的库.
The extension must be linked to the library it is using.
setup(ext_modules=cythonize([Extension(
"pca", ["pca.pyx"], language='c++'),
libraries='mlpack',
]))
可以找到所有符号,并且可以通过 ldd< .so>
检查库是否正确链接.
That all symbols can be found, and libraries linked correctly, can be checked by ldd <.so>
.
请参见编译和链接 Cython文档.
See Compiling and Linking Cython documentation.
这篇关于使用mlpack时Cython模块中的未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!