Date: 2013-02-24
Tags: python, win32, x64, distutils, visualstudio 2008 express, mingw32-x64

Python Win32 binary building and x64 cross compiling on 32bit platform

Preparing Windows build environment

Build target system

  • Python-2.7 for windows x86, x64

(or Python-2.6)

Required Operating system

  • Windows XP SP3 32bit version

Required installations

Required extra library

  • Python-2.7 for amd64's libs directory for cross-link.

Install

  1. Install VisualC++ 2008 Express SP1.

  2. Install Windows SDK 2008.

  3. Install Python-2.7.3 x86 into C:\Python27 (or anywhere).

  4. Place Python-2.7.3 amd64's libs into C:\Python27\libs-amd64.

Sample code

C:\temp\setup.py:

from distutils.core import setup, Extension

setup(
  name='spam',
  version='0.1',
  ext_modules=[Extension('spam', ['spam.c'])],
)

C:\temp\spam.c:

#include <Python.h>

static PyMethodDef spam_methods[] = {
    {NULL}  /* Sentinel */
};

PyMODINIT_FUNC
initspam(void)
{
    PyObject* m;
    m = Py_InitModule("spam", spam_methods);
}

Build python module

for x86

../../../../_images/SDK6.1-CMD.png

Microsoft Windows SDK v6.1 CMD Shell

First, invoke Windows SDK's CMD Shell from Start -> Microsoft Windows SDK v6.1 -> CMD Shell and do following:

C:\tmp> setenv /x86 /release
C:\tmp> set libpath=dummy
C:\tmp> python setup.py build

Finally, you get spam.pyd for x86 architecture in build\lib.win32-2.7.

注釈

Python2.7 distutils requires the libpath environment variable (msvc9compiler.py Line 255). But libpath is not used (msvc9compiler.py Line 371).

for x64

First, invoke CMD Shell and do following:

../../../../_images/debug-env-to-release-env.png

Windows Server 2008 x64 DEBUG environment

../../../../_images/release-env.png

Windows Server 2008 x64 RELEASE environment

C:\tmp> setenv /x64 /release
C:\tmp> set libpath=dummy
C:\tmp> python setup.py build --plat-name=win-amd64 build_ext --library_dirs=C:\Python27\libs-amd64

Finally, you get spam.pyd for x64 architecture in build\lib.win-amd64-2.7.

注釈

If you have x64 OS and Python amd64 installed, you can use python setup.py build command without options to build x64 binary.

References