2011-08-15 12 views
6

私はCythonのドキュメントを歩き、それぞれのサンプルアプリケーションを構築しています。私はCライブラリの使用法でちょっと立ち往生しています。 .soファイルを正常にビルドし、それをpythonファイル(test.py)にインポートしようとすると、次のエラーがスローされます。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 - 追加 "nm" で出力

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 

grepの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

EDIT:

ああ、あなたは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 

内容:私はこれらの3を含むディレクトリに構築

# 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 

この打撃を与えると、それはNM -mお使いの環境で示したものを参照してくださいしてください。

+0

私はちょうどセットアップし、これを構築し、それは完全に動作します。私は "nm"を実行すると同じ出力が表示されます。私が変更しなければならなかったのは、Python 3.2を使用していて、括弧を使用する必要があるため、printステートメントだけでした。 – JeremyFromEarth

+1

興味深い...悲しいことに、私はあなたのキュー拡張と何が違うのか分かりません。コードを共有できますか? – stderr

+0

本当にありがとうございます!ソースファイルは次の場所にあります。http://whiplax.com/cython_lib_wrapper.zip – JeremyFromEarth

関連する問題