1

I have been trying to use a dll library from CNES in python. My first approach was trough ctypes and i did something like:

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

This simple loading goes very smooth on windows but i have trouble on mac/linux. More specifically when i try to run the code on MacOS i get:

---------------------------------------------------------------------------
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

and in Linux(to be precise is a linux docker image on a macos host) i get:

---------------------------------------------------------------------------
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

Now it is preatty clear that there is some sort of incompatibility on Linux and Macos for this dll. My question are:

  1. why is that?
  2. is there a way to make this library more "compatible" ?

I have been looking around a bit and maybe Cython could be useful but i'm not entirely sure. Thank in advance for the help!

Someone pointed out this: Importing a dll in python on Ubuntu but even though this could be a valid workaround i would rather prefer to solve the problem at is core.

Paolo Squadrito
  • 235
  • 2
  • 9
  • 2
    Does this answer your question? [Importing a dll in python on Ubuntu](https://stackoverflow.com/questions/7876225/importing-a-dll-in-python-on-ubuntu) – chrisbyte Jul 10 '20 at 14:44
  • Well somehow yes but i would prefer to solve the problem at the root and get some less cumbersome stuffs – Paolo Squadrito Jul 10 '20 at 14:50
  • Did you contact CNES about that library? They could help you.... – Basile Starynkevitch Jul 10 '20 at 15:04
  • 1
    When the C code is compiled, it can only be executed on the platform it was compiled on. So a DLL library (compiled for Windows) is useless on Linux/MacOS, and vice versa. If you want to use the library on Linux, you need to download `propa64.so` and load it instead of the DLL file. On MacOS, you'd need a `propa64.dylib`, but it seems that the developer doesn't offer it, so your only possibility is to obtain the source code and compile it on MacOS yourself. – hoefling Jul 10 '20 at 17:07
  • @hoefling thanks that was very helpful. I will try to contact the developer for the source code like also Basile Starynkevitch suggested. But for now windows and linux will be sufficent – Paolo Squadrito Jul 10 '20 at 17:26

1 Answers1

0

As hoefling explained, *.dll libraries are only for Windows. At the site you've specified, there already are libraries built for and Linux.

enter image description here

The static lib propa64.a definitely should work on Linux, and I guess if it's for x86 64-bit platform, it should also work on MacOS.

olha
  • 2,132
  • 1
  • 18
  • 39
  • Both .a and .so works for linux (in my case a docker container on a mac host) unfortunately it does not with mac but for now windows and linux will do just fine. Thank you all for your help! – Paolo Squadrito Jul 11 '20 at 17:14