6

我有一个 C++ 库repeater.so,可以通过以下方式在 Linux 中从 Python 加载:

import numpy as np                                    
repeater = np.ctypeslib.load_library('librepeater.so', '.')

但是,当我在 Mac OS X(Snow Leopard,32 位)上编译相同的库并获取repeater.dylib,然后在 Python 中运行以下命令时:

import numpy as np                                
repeater = np.ctypeslib.load_library('librepeater.dylib', '.')

我收到以下错误:

OSError: dlopen(/mydir/librepeater.dylib, 6): no suitable image found.  Did find:
    /mydir/librepeater.dylib: mach-o, but wrong architecture

我是否必须做一些不同的事情才能在 Mac OS X 上的 Python 中加载动态库?

4

2 回答 2

11

这不仅仅是 dylib 中可用的架构的问题。这也是 Python 解释器在哪个架构中运行的问题。如果您在 OS X 10.6 中使用 Apple 提供的 Python 2.6.1,默认情况下它会在可能的情况下以 64 位模式运行。由于您说您的库被编译为 32 位,因此您需要强制 Python 在 32 位模式下运行。对于 Apple 提供的 Python,一种方法是设置一个特殊的环境变量:

$ python -c "import sys; print sys.maxint"
9223372036854775807
$ export VERSIONER_PYTHON_PREFER_32_BIT=yes
$ python -c "import sys; print sys.maxint"
2147483647

有关更多信息,请参阅 Apple 的man 1 python

于 2010-08-14T00:55:43.607 回答
4

没有。librepeater.dylib正如错误消息所说,您的 python 和文件之间存在体系结构不匹配。用于file检查架构librepeater.dylib是什么;您的 python 将使用未列出的其中之一构建。

于 2010-08-14T00:43:19.077 回答