Skip to content

Building Python extension modules with MinGW

valtron edited this page Mar 16, 2022 · 4 revisions

Here, $PY is the path of your Python 3.4+ installation from http://python.org.

  1. Setuptools assumes you have MSVC if you use Windows. To change the compiler it uses, create $HOME/pydistutils.cfg:

    [build]
    compiler=mingw32
    
    • You can also manually set the compiler each time you run setup.py: python setup.py --compiler=mingw32; but there's no way (that I found) to change it when using pip (CC=gcc pip install ... doesn't work).
  2. In $PY/Lib/distutils/cygwincompiler.py, add these lines right before the else: clause of get_msvcr:

    elif msc_ver[:2] == '19':
        # VS2015 / MSVC 14.0; works without anything, somehow
        return []

Extension modules should build now.

Common Issues

Cython

If you run into this problem regarding __pyx_check_sizeof_voidp:

something.c:238:41: warning: division by zero [-Wdiv-by-zero]
|     enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) };
|                                         ^
something.c:238:12: error: enumerator value for '__pyx_check_sizeof_voidp' is not an integer constant
|     enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) };
|            ^~~~~~~~~~~~~~~~~~~~~~~~

The fix is to add this to %PYTHON_DIR%\include\pyconfig.h:

--- pyconfig.h	2016-01-27 10:54:56.000000000 +0300
+++ pyconfig.h	2016-03-28 11:46:48.000000000 +0300
@@ -100,6 +100,12 @@
 
 /* Compiler specific defines */
 
+#ifdef __MINGW32__
+#ifdef _WIN64
+#define MS_WIN64
+#endif
+#endif
+
 /* ------------------------------------------------------------------------*/
 /* Microsoft C defines _MSC_VER */
 #ifdef _MSC_VER

Credits

  • Thanks to @alkalinin for the info that MSVC 14 doesn't need a runtime library, and about the --compiler flag to setuptools