Compiling 64-bit extension modules on Windows
Q: How do I build a Cython extension for 64-bit Python on Windows using freely available tools?
A: Use the free Windows SDK C/C++ compiler from Microsoft.
This is the vendor's platform C compiler for Windows, and the optimizing C/C++ compiler used to build the 64-bit version of Python on Windows. If you stay with this compiler, your extension will always be binary compatible with Python and installed system components and libraries.
For Python 2.6, 2.7 and 3.1, you should use the Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1. Later or earlier versions of the SDK will link with the wrong C runtime. Note that later versions of Python is likely to require a more recent version of the Windows SDK. This is required to make sure the extension is linked with the same C runtime version as Python. Using the correct version of the SDK is the only way to control this with Microsoft's build tools.
The link for the 3.5 version of the Windows SDK is:
You want the ISO file GRMSDKX_EN_DVD.iso for AMD64 (also known as x64 or x86-64). It can build 32-bit binaries for x86 as well. Windows 7 can burn ISO images to DVD, otherwise you will e.g. need Nero.
Before you run setup.py to build with distutils, do this in the SDK command window:
C:\Program Files\Microsoft SDKs\Windows\v7.0>set DISTUTILS_USE_SDK=1 C:\Program Files\Microsoft SDKs\Windows\v7.0>setenv /x64 /release
The yellow text should now turn green, and you are ready to build AMD64 Python extension modules.
Setting the DISTUTILS_USE_SDK environment variable tells distutils to use the platform C/C++ compiler. Otherwise it will try to use the compiler shipped with Microsoft Visual C++.
Observe that you do not need Microsoft Visual C++ Express.
Less useful information:
** Do not use MinGW-w64. As you will notice, the MinGW import library for Python (e.g. libpython27.a) is omitted from the AMD64 version of Python. This is deliberate. Do not try to make one using dlltool. There is no official MinGW-w64 release yet, it is still in "beta" and considered unstable, although you can get a 64-bit build from e.g. TDM-GCC. There have also been issues with the mingw runtime conflicting with the MSVC runtime; this can happen from places you don't expect, such as inside runtime libraries for g++ or gfortran. To stay on the safe side, avoid MinGW-w64 for now.
** Those with experience from Linux or Apple MacOSX might pick GCC (MinGW) as their C/C++ compiler of choise on Windows. Note that GCC is not the platform C compiler on Windows. To be binary compatible with most libraries and applications on Windows, use Microsoft's compiler. This is no different from selecting GCC as the compiler of choise on Linux. Microsoft's platform C/C++ compiler for Windows is free or charge.
** For C++, observe that GCC (Mingw) and MSVC use different C++ runtimes. If you use C++ in your extension, the C++ runtime must also be compatible with Python's C runtime. For this reason, always use Microsofts compiler for C++, even on 32-bit x86. This is very important on a platform like Windows where C++ is pervasive (cf. COM and ActiveX). If you use GCC as C++ compiler your extension will not be binary compatible with other C++ libraries or COM, not be compatible with Python's C runtime, and differences in exception handling between GCC and MSVC can crash your application.
** If you need Fortran (e.g. for f2py or fwrap), Intel, Absoft and Portland have compilers compatible with Microsoft's C compiler and linker. Be very careful if you use an unofficial MinGW-w64 build of gfortran, as libgfortran is linked against an uncompatible C runtime. Unlike C and C++, there is no freely available Fortran compiler except MinGW-w64 gfortran on Windows 64.
** If you prefer to use Microsoft Visual C++ for editing and debugging, the SDK can be registered with the commercial version of Microsoft Visual Studio. For Python 2.6, 2.7 and 2.8, you will need Visual Studio 2008.