【问题标题】:What is the meaning of this ImportError when importing a Cython generated .so file?导入 Cython 生成的 .so 文件时,此 ImportError 是什么意思?
【发布时间】:2011-10-27 11:28:05
【问题描述】:

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

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

【问题讨论】:

  • 这似乎是一个链接问题。您能否在此处重新构建并包含构建输出和用于构建的命令?
  • @Mike Steder 感谢您查看此内容,我添加了构建命令和输出
  • 好的,复制的运气不太好,所以让我们尝试更多的调试。试试nm queue.so 看看 _queue_free 旁边列出了什么。也可以使用otool -L queue.so 并查看 DYLD_LIBRARY_PATH (echo $DYLD_LIBRARY_PATH)。
  • 我应该从“nm”命令中寻找什么特别的东西吗?输出很长。我已将“otool”的输出添加到原始问题中。
  • 也许这可能是两个 calg 文件不在正确位置的问题?我尝试将它们放在 usr/include/calg & usr/lib/calg/ 中,但这导致构建失败。之后,我将它们移到项目文件夹中,并且构建工作正常。另外( echo $DYLD_LIBRARY_PATH )返回一个空行

标签: python shared-libraries cython


【解决方案1】:

编辑:

啊,你没有提到你依赖于 libcalg 中的代码。构建 cextension 时需要编译并包含这些内容。

只需修改 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 的内容:

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

test.py 的内容

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

setup.py 的内容:

# 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

请试一试,看看 nm -m 在您的环境中显示什么。

【讨论】:

  • 我刚刚设置并构建了它,它运行良好。当我运行“nm”时,我看到了相同的输出。我唯一需要更改的是 print 语句,因为我使用的是 Python 3.2,你必须使用括号。
  • 有趣...遗憾的是,我不确定您的队列扩展有什么不同。可以分享一下代码吗?
  • 非常感谢您的帮助!源文件位于:whiplax.com/cython_lib_wrapper.zip
  • 甜!我希望我可以两次投票赞成这个答案。按照编辑中的说明进行操作后,一切似乎都正常。什么战斗。谢谢!为清楚起见,通过将额外的“libcalg/queue.c”添加到该列表中,我们是否告诉 Cython 在将 c 文件链接到 queue.pyx 之前对其进行编译?
  • 对,就是这样。它现在编译两者并将它们链接到最终的 *.so 文件中。
猜你喜欢
  • 1970-01-01
  • 2016-11-08
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
相关资源
最近更新 更多