【问题标题】:Python ctypes.cdll.LoadLibrary works differently between 2.6.5 and 2.7.1Python ctypes.cdll.LoadLibrary 在 2.6.5 和 2.7.1 之间的工作方式不同
【发布时间】:2010-12-10 01:52:37
【问题描述】:

我的系统同时具有 Python 2.6.5 和 2.7.1 版本,我注意到一个 LoadLibrary 可以工作,另一个不能。

Python 2.7.1 (r271:86832, Nov 30 2010, 10:03:07)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> cdll.LoadLibrary("./mylib.so")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/ctypes/__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "/usr/local/lib/python2.7/ctypes/__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ./mylib.so: undefined symbol: compress2

在 2.6.5 土地上工作:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> cdll.LoadLibrary("./mylib.so")
<CDLL './mylib.so', handle 98bbd88 at b785c94c>

有没有人知道为什么 python 2.7.1 不工作的好方法?

【问题讨论】:

  • 我注意到,当我使用 ctypes.windll.LoadLibrary 而不是 ctypes.cdll.LoadLibrary 时,我可以加载此处创建的示例 link。类似于你的问题。 Windows 7下的Python 2.7.1。

标签: python ctypes loadlibrary


【解决方案1】:

出于某种原因,Python ctypes 对库依赖关系非常敏感。我仍然不知道为什么 2.6.5 在上面工作,但做以下修复它:

编译 mylib.so 时,我需要明确指定或链接到 zlib 库。由于“compress2”来自 zlib 库,我可以通过以下方式完成:

g++ blah blah mylib.so blah blah -lz

-lz 链接到 zlib 库,如果您执行“ldd mylib.so”,它将显示出来。这可以修复它并允许“LoadLibrary”不会失败。

【讨论】:

  • 也许 Python 2.6 出于某种原因已经在加载 zlib 而 Python 2.7 没有。然而,这显然是 mylib 声明其依赖关系的失败——它之前工作的事实纯属偶然。
猜你喜欢
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 2019-11-11
  • 2017-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-12
相关资源
最近更新 更多