115

I'm trying to compile a python extension with cython in win 7 64-bit using mingw (64-bit).
I'm working with Python 2.6 (Active Python 2.6.6) and with the adequate distutils.cfg file (setting mingw as the compiler)

When executing

> C:\Python26\programas\Cython>python setup.py build_ext --inplace

I get an error saying that gcc has not an -mno-cygwin option:

> C:\Python26\programas\Cython>python setup.py build_ext --inplace
running build_ext
skipping 'hello2.c' Cython extension (up-to-date)
building 'hello2' extension
C:\mingw\bin\gcc.exe -mno-cygwin -mdll -O -Wall -IC:\Python26\include -IC:\Python26\PC -c hello2.c -o build\temp.win-amd64-2.6\Release\hello2.o
gcc: error: unrecognized command line option '-mno-cygwin'
error: command 'gcc' failed with exit status 1

gcc is:

C:\>gcc --version
gcc (GCC) 4.7.0 20110430 (experimental)
Copyright (C) 2011 Free Software Foundation, Inc.

How could I fix it?

joaquin
  • 82,968
  • 29
  • 138
  • 152

4 Answers4

182

It sounds like GCC 4.7.0 has finally removed the deprecated -mno-cygwin option, but distutils has not yet caught up with it. Either install a slightly older version of MinGW, or edit distutils\cygwinccompiler.py in your Python directory to remove all instances of -mno-cygwin.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 10
    Thanks Ignacio. I already tried this solution by myself (to eliminate -mno-cygwin from cygwinccompyler). The error disappeared but then I got a different error with ld. But this is a different question. I will post it tomorrow. Now need some rest... why is so difficult to compile in windows? – joaquin May 17 '11 at 20:54
  • 7
    This is what worked for me. I followed Ignacio's instructions verbatim. Replaced 4 instances of the string "-mno-cygwin" with "" in the cygwincompile.py file – Ram Narasimhan Oct 27 '12 at 20:20
  • 4
    @joaquin: Did you manage to sort the `ld` error that you got? I've run into the same error, and am struggling to sort it. – robintw Mar 02 '13 at 13:45
  • @robintw yes I did. I posted a recipe at that moment (And I found it!). Please see edit – joaquin Mar 03 '13 at 10:56
  • 2
    Python 2.7.9 has solved this problem. Python 2.7.8 still has it. – fx-kirin Jan 30 '15 at 02:14
  • @Ignacio Vazquez-Abrams I have a [Cython post](http://stackoverflow.com/questions/41944883/verifying-compatibility-in-compiling-extension-types-and-using-them-with-cdef) you may be able to provide insight on. – ballade4op52 Jan 31 '17 at 20:47
11

During the process of solving these and the following problems I found, I wrote a recipe in this thread. I reproduce it here in case it could be of utility for others:


Step by step recipe to compile 64-bit cython extensions with python 2.6.6 with mingw compiler in win 7 64-bit

Install mingw compiler
1) Install tdm64-gcc-4.5.2.exe for 64-bit compilation

Apply patch to python.h
2) Modify python.h in C:\python26\include as indicated in http://bugs.python.org/file12411/mingw-w64.patch

Modify distutils
Edit 2013: Note than in python 2.7.6 and 3.3.3 -mno-cygwin has been finally removed so step 3 can be skipped.

3) Eliminate all the parameters -mno-cygwin fom the call to gcc in the Mingw32CCompiler class in Python26\Lib\distutils\cygwinccompiler.py
4) In the same module, modify get_msvcr() to return an empty list instead of ['msvcr90'] when msc_ver == '1500' .

Produce the libpython26.a file (not included in 64 bit python)
Edit 2013: the following steps 5-10 can be skipped by downloading and installing libpython26.a from gohlke.

5) Obtain gendef.exe from mingw-w64-bin_x86_64- mingw_20101003_sezero.zip (gendef.exe is not available in the tmd64 distribution. Another solution is to compile gendef from source...)
6) Copy python26.dll (located at C\windows\system32) to the user directory (C:\Users\myname)
7) Produce the python26.def file with:

gendef.exe C:\Users\myname\python26.dll

8) Move the python.def file produced (located in the folder from where gendef was executed) to the user directory
9) Produce the libpython.a with:

dlltool -v --dllname python26.dll --def C:\Users\myname \python26.def --output-lib C:\Users\myname\libpython26.a

10) Move the created libpython26.a to C:\Python26\libs

Produce your .pyd extension
11) Create a test hello.pyx file and a setup.py file as indicated in cython tutorial (http://docs.cython.org/src/quickstart/build.html)
12) Compile with

python setup.py build_ext --inplace

Done!

joaquin
  • 82,968
  • 29
  • 138
  • 152
  • I did step 1 on win64 python27. Then I tried and cython installed successfully. I had no idea what to do with the patch in step 2. – craastad Mar 04 '13 at 11:13
  • @Chris Raastad. Interesting. But this is not about cython installing but cython compiling a script. Not sure if you meant that. Anyway, two days ago there was a comment from robintw about having the ld error. So, if you actually have mingw working, maybe it depends on the version you install and on maybe the libraries you already have installed that you go throu at first shot or you get stuck with errors. – joaquin Mar 04 '13 at 23:43
  • 1
    +1 This is a useful answer. For extensions using numpy, I also had to hack options inside the `numpy/distutils` folder, both for `mingw32compiler.py`. – Caleb Hattingh Oct 27 '13 at 22:14
  • In python27 it seems you have to define MS_WIN64 by adding it to the cygwinccompiler.py file. See here: http://stackoverflow.com/a/19867426/2136991 – David Nov 08 '13 at 19:51
  • @David in the question you link the OP refers to python26, not python27. Did you follow *exactly* my recipe and still needed to made the change you refer? – joaquin Nov 08 '13 at 21:39
  • @joaquin I don't think the patch you mention in step 2 can be applied to the python.h in 2.7.5: https://gist.github.com/dstuebe/7385979 The change I made in cygwinccompiler.py seems to do the same thing a different way. If there is a better way to do it I am all for it! – David Nov 09 '13 at 14:27
  • @joaquin I have a [Cython post](http://stackoverflow.com/questions/41944883/verifying-compatibility-in-compiling-extension-types-and-using-them-with-cdef) you may be able to provide insight on. – ballade4op52 Jan 31 '17 at 20:48
9

This bug has now been fixed in Python 2.7.6 release candidate 1.

The patching commit is here.

The resolved issue tracker thread is here.

Michael
  • 5,994
  • 7
  • 44
  • 56
-2

Try this . It really works for the error
https://github.com/develersrl/gccwinbinaries

Selva
  • 1