2011-08-15 2 views
6

저는 Cython 문서를보고 각 예제 응용 프로그램을 작성 중입니다. 나는 C 라이브러리 사용에 조금 머물러있다. .so 파일을 성공적으로 빌드하고 test.py라는 python 파일로 가져 오려고하면 다음 오류가 발생합니다.Cython에서 생성 한 .so 파일을 가져올 때이 ImportError의 의미는 무엇입니까?

$ python3.2 test.py 
Traceback (most recent call last): 
    File "test.py", line 12, in <module> 
    from queue import Queue 
ImportError: dlopen(/Users/jeremy/Development/labs/python/cython_lib_wrapper/queue.so, 2): Symbol not found: _queue_free 
    Referenced from: /Users/jeremy/Development/labs/python/cython_lib_wrapper/queue.so 
    Expected in: flat namespace 
in /Users/jeremy/Development/labs/python/cython_lib_wrapper/queue.so 

.so 파일은 test.py 파일 옆에 있습니다. 그래서 그것은 발견되어야하는 것처럼 보입니다. 이것은 Cython의 최신 버전을 실행하며, OSX 10.6에서 Python 3.2를 사용합니다.

통찰력이 있으십니까?

편집 - 명령과 출력을 구축 추가

$ python3.2 setup.py build_ext --inplace 
running build_ext 
cythoning queue.pyx to queue.c 
building 'queue' extension 
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -O3 -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -I/Library/Frameworks/Python.framework/Versions/3.2/include/python3.2m -c queue.c -o build/temp.macosx-10.6-intel-3.2/queue.o 
    queue.c: In function ‘__pyx_f_5queue_5Queue_append’: 
    queue.c:627: warning: cast to pointer from integer of different size 
    queue.c: In function ‘__pyx_f_5queue_5Queue_extend’: 
    queue.c:740: warning: cast to pointer from integer of different size 
    queue.c: In function ‘__pyx_f_5queue_5Queue_peek’: 
    queue.c:813: warning: cast from pointer to integer of different size 
    queue.c: In function ‘__pyx_f_5queue_5Queue_pop’: 
    queue.c:965: warning: cast from pointer to integer of different size 
    gcc-4.2 -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -isysroot /Developer/SDKs/MacOSX10.6.sdk -g build/temp.macosx-10.6-intel-3.2/queue.o -o 

편집 2 - cmd를 코멘트에 요청 "otool"추가

queue.so: 
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0) 

편집 3 - 추가 "나노"출력

U ___stack_chk_fail 
U ___stack_chk_guard 
U _queue_free 
U _queue_is_empty 
U _queue_new 
U _queue_peek_head 
U _queue_pop_head 
U _queue_push_tail 
U dyld_stub_binder 

그렙 cmd를 출력이 :

(undefined) external _queue_free (dynamically looked up) 
+2

이것은 연결 문제처럼 보입니다. 다시 빌드하고 빌드 출력과 여기서 빌드하는 데 사용 된 명령을 모두 포함 할 수 있습니까? – stderr

+0

@Mike Steder 이걸 살펴 줘서 고마워, 내가 빌드 명령과 출력을 추가했습니다. – JeremyFromEarth

+1

좋아요, 재수 없기 때문에 재연을 해보 죠. 'nm queue.so'를 시도하고 _queue_free 옆에 나열된 것을보십시오. 또한'otool -L queue.so'를 사용하고 DYLD_LIBRARY_PATH ('echo $ DYLD_LIBRARY_PATH')를 확인하십시오. – stderr

답변

5

편집 :

아, 당신은 당신이 libcalg의 코드에 대한 종속성을 가지고 언급하지 않았다. 그 물건은 당신이 cextension을 만들 때 컴파일되고 포함될 필요가있다.

# setup.py 
# ... 
ext_modules = [Extension("queue", ["queue.pyx", "libcalg/queue.c"])] 
# ... 

우리는 뒤로 물러나 당신은 정말 간단한 예를 구축 할 수 있는지 볼 수 있습니다 : 나는 다음 (3 개 파일을 시도했습니다

을 myext

그냥 setup.py를 수정 .pyx, test.py, setup.py) 제대로 작동하는 것 같습니다. 물론 OS X 10.7을 사용하고 있으므로 사용자 환경과 정확히 동일하지 않습니다. 차이점을 배제하기 위해 아마도 이것을 복사하여 온전한 체크로 만들 수 있습니다. myext.pyx의

내용 : setup.py의 test.py

# test.py 
from myext import square 
print "%d squared is %d"%(4, square(4)) 

내용의

# myext.pyx 
def square(x): 
    return x * x 

내용 : 나는이 세 들어있는 디렉토리 구축

# setup.py 
from distutils.core import setup 
from distutils.extension import Extension 
from Cython.Distutils import build_ext 

ext_modules = [Extension("myext", ["myext.pyx"])] 

setup(
    name = 'Hello world app', 
    cmdclass = {'build_ext': build_ext}, 
    ext_modules = ext_modules 
) 

파일 :

cython_test$ /usr/bin/python setup.py build_ext --inplace 
running build_ext 
cythoning myext.pyx to myext.c 
building 'myext' extension 
creating build 
creating build/temp.macosx-10.7-intel-2.7 
llvm-gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c myext.c -o build/temp.macosx-10.7-intel-2.7/myext.o 
llvm-gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup -Wl,-F. -arch i386 -arch x86_64 build/temp.macosx-10.7-intel-2.7/myext.o -o /Users/steder/SO/cython_test/myext.so 

cython_test$ python test.py 
4 squared is 16: 

내 환경은 유사한 otool 출력을 가지고 있으며 DYLD_LIBRARY_PATH도 설정 해제되어 있지만 nm -m은 정의 된대로 제곱을 표시합니다.특히

:

00000000000011d0 (__DATA,__data) non-external ___pyx_k__square 
00000000000011e0 (__DATA,__data) non-external ___pyx_mdef_5myext_square 
0000000000001218 (__DATA,__bss) non-external ___pyx_n_s__square 
0000000000000c80 (__TEXT,__text) non-external ___pyx_pf_5myext_square 

이에게 기회를 제공하고 나노 -m 사용자 환경에서 보여줍니다 무엇을 참조하십시오.

+0

방금 ​​설치하고 완성했으며 완벽하게 작동합니다. 나는 "nm"을 실행할 때 같은 출력을 보았습니다. 내가 바꿔야 만 한 것은 print 문이었습니다. 왜냐하면 저는 파이썬 3.2를 사용하고 있고 괄호를 사용해야하기 때문입니다. – JeremyFromEarth

+1

흥미 롭습니다 ... 슬프게도, 당신의 큐 확장과 다른 점이 확실하지 않습니다. 코드를 공유 할 수 있습니까? – stderr

+0

정말 고맙습니다. 소스 파일은 다음 위치에 있습니다. http://whiplax.com/cython_lib_wrapper.zip – JeremyFromEarth

관련 문제