【问题标题】:How use correctly a .dll file in python on linux and mac如何在 linux 和 mac 上的 python 中正确使用 .dll 文件
【发布时间】:2020-10-31 08:07:39
【问题描述】:

我一直在尝试在 python 中使用来自CNES 的 dll 库。 我的第一种方法是通过ctypes,我做了类似的事情:

from ctypes import *
cdll.LoadLibrary("dll/propa64.dll")

这个简单的加载在 Windows 上非常顺利,但我在 mac/linux 上遇到了麻烦。更具体地说,当我尝试在 MacOS 上运行代码时,我得到:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-6-74c6935de494> in <module>
----> 1 cdll.LoadLibrary("dll/propa64.dll")

~/opt/anaconda3/lib/python3.7/ctypes/__init__.py in LoadLibrary(self, name)
    440
    441     def LoadLibrary(self, name):
--> 442         return self._dlltype(name)
    443
    444 cdll = LibraryLoader(CDLL)

~/opt/anaconda3/lib/python3.7/ctypes/__init__.py in __init__(self, name, mode, handle, use_errno, use_last_error)
    362
    363         if handle is None:
--> 364             self._handle = _dlopen(self._name, mode)
    365         else:
    366             self._handle = handle

OSError: dlopen(dll/propa64.dll, 6): no suitable image found.  Did find:
    dll/propa64.dll: unknown file type, first eight bytes: 0x4D 0x5A 0x90 0x00 0x03 0x00 0x00 0x00
    /range/code_python/propa/dll/propa64.dll: unknown file type, first eight bytes: 0x4D 0x5A 0x90 0x00 0x03 0x00 0x00 0x00

在 Linux 中(准确地说是 macos 主机上的 linux docker 映像)我得到:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-3-74c6935de494> in <module>
----> 1 cdll.LoadLibrary("dll/propa64.dll")

/opt/conda/lib/python3.7/ctypes/__init__.py in LoadLibrary(self, name)
    440 
    441     def LoadLibrary(self, name):
--> 442         return self._dlltype(name)
    443 
    444 cdll = LibraryLoader(CDLL)

/opt/conda/lib/python3.7/ctypes/__init__.py in __init__(self, name, mode, handle, use_errno, use_last_error)
    362 
    363         if handle is None:
--> 364             self._handle = _dlopen(self._name, mode)
    365         else:
    366             self._handle = handle

OSError: dll/propa64.dll: invalid ELF header

现在很明显,这个 dll 在 Linux 和 Macos 上存在某种不兼容。 我的问题是:

  1. 这是为什么呢?
  2. 有没有办法让这个库更“兼容”?

我一直在环顾四周,也许 Cython 可能有用,但我不完全确定。 提前感谢您的帮助!

有人指出这一点:Importing a dll in python on Ubuntu,但即使这可能是一个有效的解决方法,我还是宁愿解决核心问题。

【问题讨论】:

  • 这能回答你的问题吗? Importing a dll in python on Ubuntu
  • 嗯,是的,但我更愿意从根本上解决问题并获得一些不那么繁琐的东西
  • 你就那个图书馆联系过 CNES 吗?他们可以帮助你......
  • C代码编译时,只能在编译时所在的平台上执行。所以 DLL 库(为 Windows 编译)在 Linux/MacOS 上是无用的,反之亦然。如果要在 Linux 上使用该库,则需要下载 propa64.so 并加载它,而不是加载 DLL 文件。在 MacOS 上,你需要一个propa64.dylib,但开发人员似乎没有提供它,所以你唯一的可能是获取源代码并自己在 MacOS 上编译。
  • @hoefling 谢谢,这很有帮助。我将尝试联系开发人员以获取源代码,就像 Basile Starynkevitch 建议的那样。但是现在windows和linux就足够了

标签: python python-3.x linux macos dll


【解决方案1】:

正如hoefling 解释的那样,*.dll 库仅适用于 Windows。 在您指定的站点上,已经有为 Linux 构建的库。

静态库propa64.a 肯定可以在Linux 上运行,我猜如果它是用于x86 64-bit 平台的,它也应该可以在MacOS 上运行。

【讨论】:

  • .a 和 .so 都适用于 linux(在我的情况下是 mac 主机上的 docker 容器),不幸的是它不适用于 mac,但现在 windows 和 linux 就可以了。谢谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 2016-02-01
  • 2016-01-06
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多