Search code examples
pythonwindowscygwin

Calling Cygwin from python fails with: error while loading shared libraries: ?: cannot open shared object file: No such file or directory


I have a C program compiled with GCC using Cygwin, which I want to run from a python program in windows 7. I got as far as using the subprocess module, but was getting the "error while loading shared libraries..." and no other information.

Googling resulted in the following two questions; one with no answer and one with an accepted answer that didn't (on first reading) fully explain how to fix it, and a comment promising a full solution from almost two years ago.

error while loading shared libraries: ?: cannot open shared object file: No such file or directory

Launching CYGWIN-built executable from Java on Windows 7 fails with "error while loading shared libraries: ?: No such file or directory"

I've in fact fixed my issue, but I wanted to document it here in case anyone else has the same issue.


Solution

  • The accepted answer to the second question is correct (at least in my case) - it's all to do with where the working directory is, and whether it contains the cygwin dlls.

    I was calling C:\\cygwin\\bin\\bash.exe from the working directory of my python program, meaning bash.exe was looking for whatever libraries it needed in the python working directory, the libraries being located in C:\cygwin\bin.

    Since I'm using Popen in python, if I include the argument cwd="C:\\cygwin\\bin\\" in the Popen call, it starts bash.exe with the current working directory set to cygwin\bin, and everything works fine.

    EDIT: Alternatively, copying the relevant dlls to the working directory of the python program will have the same effect. However, copying just cygwin1.dll doesn't work (at least for me), and there are a lot of other cygwin dlls. If you can work out which ones you need and just copy them (or copy all of them) then that also works.