2011-08-15 47 views
6

我正在浏览Cython文档并构建每个示例应用程序。我有点卡在使用C库。成功构建.so文件并尝试将其导入名为test.py的python文件后,会引发以下错误。导入Cython生成的.so文件时,导入错误的含义是什么?

$ 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 - 加入 “otool” CMD请求在评论

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

编辑:

啊,你没有提到你对在libcalg代码的依赖。这些东西需要编译并包含在构建扩展时。

只需修改setup.py:

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

我们可以退一步,看看你可以建立一个非常简单的例子:

我试过以下(3个文件,myext .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 
) 

我建立在含有这些3的目录文件:

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”时,我看到相同的输出。我唯一需要改变的是打印语句,因为我使用的是Python 3.2,你必须使用parens。 – JeremyFromEarth

+1

有趣......不幸的是,我不确定你的队列扩展有什么不同。你能分享代码吗? – stderr

+0

我真的很感谢你的帮助!源文件位于:http://whiplax.com/cython_lib_wrapper.zip – JeremyFromEarth