2014-09-30 2 views
2

을 가져올 수 없습니다. 지난 며칠 동안 rnnlib을 공유 라이브러리로 컴파일했습니다. 그것은 C++ 라이브러리입니다. 파이썬에서 호출하고 싶습니다. 내 선택은 cython에 떨어졌다. 그래서 저는 C++ 함수를 만들었습니다. void libCall(int argc, char* argv[]) 실제로 rnnlib의 주요 기능과 동일하지만 더 쉽게 호출 할 수 있도록 이름이 바뀌 었습니다. rnnlib 라이브러리는/usr/lib에Python이 (cython) 공유 라이브러리

rnn.pyx

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

내가보고 추가 테스트 파일을 만든 그

# distutils: language = c++ 

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

cpdef call(): 
    print 'hallo welt' 

setuprnn.py 모양입니다 라이브러리를 호출 할 수있는 경우. Test.cpp에

#include <iostream> 
#include "libcall.hpp" 

using namespace std; 


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

지금은

로 Test.cpp에를 빌드 할 때 g ++ -Wall -I./src/ Test.cpp에 -lrnnlib -lnetcdf_C++ -lnetcdf -lm -lstdC++ - o 테스트

나는 그것을 실행할 수 있고 모든 것이 작동한다. 그리고 나는 python setuprnn.py build_ext -i를 실행할 때 rnn.so와 rnn.cpp를 얻습니다. 내가 파이썬을 실행하고 나노와 rnn.so에 나는 보았다 import rnn

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
ImportError: ./rnn.so: undefined symbol: _ZNK5NcVar3getEPcPKl 

입력이 왔을 때 그러나 그래서

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

도'LDD ./rnn.so의 출력을 첨부 해주세요 :) 많은 시간을 낭비하지 않는 미래 세대 도움이되기를 바랍니다

#!/bin/sh cython ... gcc ... g++ ... -o rnn.so -lnetcdf_c++ -lnetcdf -lm -lstdc++ -lrnnlib 

:이 솔루션은 단지 라이브러리를 생성하기위한 쉘 스크립트를 생성 한 후입니다 '및'sudo ldconfig -p | grep netcdf' 명령을 사용하십시오. – aponomarenko

답변

0

내가 전에 게시하지 않아서 미안해, 지금 당장 바쁘다. 그래서 일반적으로 네가 옳다. 그것은 작동해야합니다.문제는 내 라이브러리의 종속성입니다. Cython은 그것을 잡지 않습니다.

위 (setuprnn.py)에서 파이썬 스크립트는 아무것도하지 않습니다하지만 실행 :

cython ... 
gcc ... 
g++ ... -lrnnlib -lnetcdf -lstdc++ -o rnn.so 

이 엔트리의 게시물에 위에 언급 한 바와 같이 문제를 만듭니다. 나는이

1

문제점을 복제 할 수 없습니다. 여기

#!/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 또는 다른 라이브러리에서 인용 한 내용이 없습니다. 다른 사람들이이를 테스트 할 수 있도록 설명을 업데이트 할 수 있습니까?