Python ctypes.cdll.LoadLibrary 在 2.6.5 和 2.7.1 之间的工作方式不同

标签 python ctypes loadlibrary

我有一个同时具有 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 不工作的好方法?

最佳答案

出于某种原因,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 ctypes.cdll.LoadLibrary 在 2.6.5 和 2.7.1 之间的工作方式不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4404974/

相关文章:

Python:复制 `array.array`

C++: list 和从不同目录动态加载 DLL

c++ - 通过 LoadLibrary 从纯 C++ 程序加载托管 C++ DLL

python - 函数在无意时修改输入

python - 执行 `from abc import xyz` 模块 `abc`去哪了?

c++ - 无法创建 char * 对象以发送到 c++/c 共享对象库

c - 使用Python返回指向已经存在的内存地址的指针

python - IPython 支持 ctypes 吗?

c++ - 多次加载同一个dll

python - 使用 python 的 oauth 谷歌