Appendix: Installing MinGW on Windows



  1. Download the MinGW installer from
    http://www.mingw.org/wiki/HOWTO_Install_the_MinGW_GCC_Compiler_Suite.
    (As of this
    writing, the download link is a bit difficult to find; it’s under
    “About” in the menu on the left-hand side). You want the file
    entitled “Automated MinGW Installer” (currently version 5.1.4).



  2. Run it and install MinGW. Only the basic package is strictly
    needed for Cython, although you might want to grab at least the
    C++ compiler as well.



  3. You need to set up Windows’ “PATH” environment variable so that
    includes e.g. “c:\mingw\bin” (if you installed MinGW to
    “c:\mingw”). The following web-page describes the procedure
    in Windows XP (the Vista procedure is similar):
    https://support.microsoft.com/kb/310519



  4. Finally, tell Python to use MinGW as the default compiler
    (otherwise it will try for Visual C). If Python is installed to
    “c:\Python27”, create a file named
    “c:\Python27\Lib\distutils\distutils.cfg” containing:




    1. [build]
      compiler = mingw32






The [WinInst] wiki page contains updated information about thisprocedure. Any contributions towards making the Windows installprocess smoother is welcomed; it is an unfortunate fact that none ofthe regular Cython developers have convenient access to Windows.

Python 3.8+

Since Python 3.8, the search paths of DLL dependencies has been reset.(changelog)

Only the system paths, the directory containing the DLL or PYD fileare searched for load-time dependencies.Instead, a new function os.add_dll_directory()was added to supply additional search paths. But such a runtime update is not applicable in all situations.

Unlike MSVC, MinGW has its owned standard libraries such as libstdc++-6.dll,which are not placed in the system path (such as C:\Windows\System32).For a C++ example, you can check the dependencies by MSVC tool dumpbin:

  1. > dumpbin /dependents my_gnu_extension.cp38-win_amd64.pyd
  2. ...
  3. Dump of file my_gnu_extension.cp38-win_amd64.pyd
  4.  
  5. File Type: DLL
  6.  
  7. Image has the following dependencies:
  8.  
  9. python38.dll
  10. KERNEL32.dll
  11. msvcrt.dll
  12. libgcc_s_seh-1.dll
  13. libstdc++-6.dll
  14. ...

These standard libraries can be embedded via static linking, by adding the following options to the linker:

  1. -static-libgcc -static-libstdc++ -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive

In setup.py, a cross platform config can be added throughextending build_ext class:

  1. from setuptools import setup
  2. from setuptools.command.build_ext import build_ext
  3.  
  4. link_args = ['-static-libgcc',
  5. '-static-libstdc++',
  6. '-Wl,-Bstatic,--whole-archive',
  7. '-lwinpthread',
  8. '-Wl,--no-whole-archive']
  9.  
  10. ... # Add extensions
  11.  
  12. class Build(build_ext):
  13. def build_extensions(self):
  14. if self.compiler.compiler_type == 'mingw32':
  15. for e in self.extensions:
  16. e.extra_link_args = link_args
  17. super(Build, self).build_extensions()
  18.  
  19. setup(
  20. ...
  21. cmdclass={'build_ext': Build},
  22. ...
  23. )
[WinInst]https://github.com/cython/cython/wiki/CythonExtensionsOnWindows