0

Problem

I am trying to link C++ with python, so I compiled a .so file via g++ (the MinGW version, so I believe it should be able to run on Windows) and then in python:

import ctypes
lib = ctypes.cdll.LoadLibrary("support.so")

I get the Error:

FileNotFoundError: Could not find module 'support.so' (or one of its dependencies). Try using the full path with constructor syntax.

I tried using the full path and using os.add_dll_directory(), but didn't help.

Environment

Python 3.8 / 3.9 both not working

The .so file is created by myself, it is compiled via g++.

I encourtered this problem first on Windows 11, and solved it by changing the winmode in ctypes.__init__() by the help of this answer https://stackoverflow.com/a/64472088/9249608.

However, when I copy this program and the .so file to another computer with Window 10, I cannot solve this problem by any of the above methods. I even tried adding the folder to the PATH or copying this .so file to the python folder, but it still produces the error.

Is there any way that I can know exactly which paths are being searched? I see that there are many searching modes in ctypes.__init__() but I don't know how to output the corresponding searching paths.

Serena
  • 19
  • 3
  • 1
    *support.so* looks like *Nix* file. Are you sure it's supposed to run on *Win*? How was it created? Check it with https://www.dependencywalker.com. – CristiFati Mar 20 '22 at 22:15
  • @CristiFati I am sure because I compiled on Win. (The linux version cannot run on Win but the Win 11 compiled .so can run on Win 11, while I cannot run anything on Win 10 even I recompile on Win 10). I created a dummy .so which contains almost nothing but python still cannot find it... – Serena Mar 21 '22 at 01:13
  • @CristiFati Thanks and I have used dependencywalker and find that the .so file actually relies on the compiler environment, e.g. c:\program files\msys64\mingw64\bin\LIBSTDC++-6.DLL. Could you possibly suggest what should I do if I want to give the .so to another user without forcing them to install a compiler? I see that there are plenty of .dll denpendencies, and it seems to be very strange if I copy all of the dlls to the other user. – Serena Mar 21 '22 at 05:52
  • 1
    Use *-static-libstdc++* when building it. https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/Link-Options.html. – CristiFati Mar 21 '22 at 06:52

0 Answers0