最近几天,我设法将rnnlib编译为共享库。这是一个c++库。我想从python调用它。我的选择落到了赛昂。所以我创建了一个c++函数void libCall(int argc, char* argv[])实际上与rnnlib的主要功能相同,但已重命名以使其易于调用。 rnnlib库是/ usr / lib

我的 rnn.pyx 像这样

# distutils: language = c++

cdef extern from "libcall.hpp":
    void libCall(int argc, char* argv[])

cpdef call():
    print 'hallo welt'

我的设置rnn.py
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
import os

os.environ["CC"] = "gcc"
os.environ["CXX"] = "g++"
os.environ["CFLAGS"]="-I./src/"

setup(ext_modules = cythonize(
       "rnn.pyx",            # our Cython source
       libraries=["rnnlib","netcdf_c++","netcdf","m","stdc++"],  # additional source file(s)
       language="c++",             # generate C++ code
      ))

我创建了一个额外的测试文件,以查看是否可以调用该库。
test.cpp
#include <iostream>
#include "libcall.hpp"

using namespace std;


int main(int argc, char* argv[])
{
    libCall(argc, argv);
}

现在当我用以下命令构建test.cpp时



我可以运行它,一切正常。
当我运行python setuprnn.py build_ext -i时,我得到rnn.so和rnn.cpp,这很好。但是当我运行python并输入import rnn
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./rnn.so: undefined symbol: _ZNK5NcVar3getEPcPKl

我用nm调查rnn.so并得到了这个:
000000000003f140 W _ZNK5Mdrnn5printERSo
             U _ZNK5NcDim4sizeEv
             U _ZNK5NcVar3getEPcPKl

所以我假设常量存在于库中?

我不知道为什么。我发现了类似的线程Python ImportError - undefined symbol - for custom C++ module,但不知道如何在此处应用它:
python setuprnn.py build_ext -i
Compiling rnn.pyx because it changed.
Cythonizing rnn.pyx
running build_ext
building 'rnn' extension
gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -I./src/ -fPIC -I/usr/include/python2.7 -c rnn.cpp -o build/temp.linux-x86_64-2.7/rnn.o
......

g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -I./src/ build/temp.linux-x86_64-2.7/rnn.o -o /media/psf/dfki/test/rnn.so

当我搜索它。我只找到这个网站http://upstream.rosalinux.ru/compat_reports/netcdf/3.4_to_3.5.0/abi_compat_report.html

所以我想到了向后兼容性问题,这就是为什么我安装了netcdf库4.1.3的原因,因为这是完全向后兼容的

我希望有人能帮助我,因为这确实令人沮丧。

最佳答案

我无法复制您的问题。这是我创建的脚本,用于创建文件,构建扩展名并在python中运行代码:

#!/bin/sh -e

echo "Cleaning up /tmp"
rm -f /tmp/rnn.pyx /tmp/rnn.so /tmp/setuprnn.py /tmp/libcall.hpp /tmp/rnn.cpp /tmp/rnn.so
rm -rf /tmp/build/

echo "Creating /tmp/rnn.pyx"
cat > /tmp/rnn.pyx << EOF
# distutils: language = c++

cdef extern from "libcall.hpp":
    void libCall(int argc, char* argv[])

cpdef call():
    print 'hallo welt'
EOF

echo "Creating /tmp/setuprnn.py"
cat > /tmp/setuprnn.py << EOF
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
import os

os.environ["CC"] = "gcc"
os.environ["CXX"] = "g++"
os.environ["CFLAGS"]="-I./src/"

setup(ext_modules = cythonize(
       "rnn.pyx",            # our Cython source
       libraries=["rnnlib","netcdf_c++","netcdf","m","stdc++"],  # additional source file(s)
       language="c++",             # generate C++ code
      ))
EOF

echo "Creating /tmp/libcall.hpp"
touch /tmp/libcall.hpp

echo "Building rnn.so extension"
python setuprnn.py build_ext -i

python -c "import rnn; print('-' * 30); print('Imported rnn'); print(dir(rnn))"

这是我得到的结果:
Cleaning up /tmp
Creating /tmp/rnn.pyx
Creating /tmp/setuprnn.py
Creating /tmp/libcall.hpp
Building rnn.so extension
Compiling rnn.pyx because it changed.
Cythonizing rnn.pyx
running build_ext
building 'rnn' extension
creating build
creating build/temp.linux-x86_64-2.7
gcc -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -I./src/ -fPIC -I/usr/include/python2.7 -c rnn.cpp -o build/temp.linux-x86_64-2.7/rnn.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -I./src/ build/temp.linux-x86_64-2.7/rnn.o -o /tmp/rnn.so
------------------------------
Imported rnn
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__', 'call']

在我看来很好。当然,我没有正确实现所有代码,没有写任何内容写在netcdf或您引用的其他库中。也许您可以更新您的描述,以便其他人可以测试一下?

关于python - Python无法导入(cython)共享库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26120236/

10-13 00:53