How to tell distutils to use gcc?

I just took a look at the distutils source, and the --compiler option expects "unix", "msvc", "cygwin", "mingw32", "bcpp", or "emx". It checks the compiler name you want by checking the CC environment variable. Try calling build like this:

CC=gcc python setup.py build

You don't need to set CXX, it doesn't check for that.


According to this wiki, Python versions after 3.4 do not support MinGW anymore. CPython 3.7 for Windows is compiled with MSC v.1916. When I try to use above-mentioned method with distutils.cfg, I then get an error from distutils: Unknown MS Compiler Version 1916. Looks like it has a hardcoded table of msvcr libraries in its cygwincompiler.py file (which is also responsible for MinGW), and last version known to that file is 1600 from VS2010 / MSVC 10.0.


Try setting the "CC" environment variable from inside the setup.py with os.environ.


Just in case some others are facing the same problem under Windows (where CC environment variable wouldn't have any effect) :

  • Create file "C:\Python27\Lib\distutils\distutils.cfg" and write this inside :

Code :

[build]
compiler = mingw32
  • Remove all instances of "-mno-cygwin" gcc option from file "C:\Python27\Lib\distutils\cygwinccompiler.py" :

This :

    self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
                         compiler_so='gcc -mno-cygwin -mdll -O -Wall',
                         compiler_cxx='g++ -mno-cygwin -O -Wall',
                         linker_exe='gcc -mno-cygwin',
                         linker_so='%s -mno-cygwin %s %s'
                                    % (self.linker_dll, shared_option,
                                       entry_point))

Becomes this :

self.set_executables(compiler='gcc -O -Wall',
                     compiler_so='gcc -mdll -O -Wall',
                     compiler_cxx='g++ -O -Wall',
                     linker_exe='gcc',
                     linker_so='%s %s %s'
                                % (self.linker_dll, shared_option,
                                   entry_point))

The second point can be necessary in case you are using a recent version of gcc, where the deprecated option -mno-cygwin has been removed.

Hope this will help even if it is not directly related to the OP real needs (but still related to the question's title...)