最近几天,我设法将rnnlib编译为共享库。它是一个c ++库。我想从python中调用它。我的选择落到了cython上。所以我创建了一个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'
我的 setuprnn.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.cppg ++ -Wall -I./src/ test.cpp -lnnnlib -lnetcdf_c ++ -lnetcdf -lm -lstdc ++ -o test
我可以运行它,一切正常。
当我运行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的原因,因为这是完全向后兼容的
我希望有人可以帮助我,因为这真的令人沮丧。
答案 0 :(得分:1)
我无法复制你的问题。这是我创建的用于创建文件,构建扩展以及在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或你引用的其他库中写的内容。也许你可以更新你的描述,以便其他人可以测试一下吗?
答案 1 :(得分:0)
上面的python脚本(setuprnn.py)除了执行此操作外别无其他:
cython ...
gcc ...
g++ ... -lrnnlib -lnetcdf -lstdc++ -o rnn.so
这会在条目帖中产生上述问题。然后解决方案就是创建一个用于创建库的shell脚本:
#!/bin/sh
cython ...
gcc ...
g++ ... -o rnn.so -lnetcdf_c++ -lnetcdf -lm -lstdc++ -lrnnlib
我希望这有助于后代不要浪费很多时间:)