簡體   English   中英

如何在 linux 和 mac 上的 python 中正確使用 a.dll 文件

[英]How use correctly a .dll file in python on linux and mac

我一直在嘗試在 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

現在很明顯,Linux 和 Macos 對於這個 dll 存在某種不兼容。 我的問題是:

  1. 這是為什么?
  2. 有沒有辦法讓這個庫更“兼容”?

我一直在環顧四周,也許 Cython 可能有用,但我不完全確定。 提前感謝您的幫助!

有人指出這一點: Importing a dll in python on Ubuntu但即使這可能是一個有效的解決方法,我還是寧願解決核心問題。

正如hoefling所解釋的,*.dll 庫僅適用於 Windows。 在您指定的站點上,已經有為 Linux 構建的庫。

在此處輸入圖像描述

static lib propa64.a絕對應該適用於 Linux,我猜如果它適用於x86 64-bit平台,它也應該適用於 MacOS。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM