notes‎ > ‎

Python module cross compiling


This was written in 2003.  It is reproduced here in case it is still useful, but expect things to have changed by now.

Some Random Babbling

Up until now, there has not been much/any information on the web about cross compiling python modules. I hope to rectify (at least a sub-set of) that problem.

This page focuses on cross-compiling on a linux host to a win32 target, using the mingw32 cross-compiler. (If you run debian, simply apt-get install mingw32.) The information was mainly gleaned from Instructions for Python Extensions with GCC/mingw32 which focuses on building modules with the win32 native mingw32 compiler. In addition, all the files available there are only for Python 1.5.

Ideally, one could just specify some sort of cross-compilation arg to distutils. However (as of Python 2.3), no such option exists. Therefore, we will have to make do with a Makefile.

There are 3 general things that must be taken into account when cross-compiling:

  • Using the cross-compiler and related tools instead of the standard ones.
  • Using the target's libraries and includes instead of the host's.
  • Mingw32 specific: mingw32 doesn't accept the .lib files that are included in the standard win32 python distribution. A slightly modified version of the script from the previously mentioned page is needed to solve this one.

The Meat

  • First, get the Python win32 installer. You can either install it on an existing windows partition(dual-boot), with vmware, wine, or just extract the relevant bits with unzip.
  • Grab my updated, example Makefile, and example .def file.
  • Modify the neccessary bits in the makefile:
    • CC, NM, DLLWRAP, DLLTOOL: The cross-compiler and its tools.
    • PYVER: The target version of python, in <MAJOR><MINOR> form.
    • PYDIR: Where you installed the target version of python.
    • INCLUDES, LDFLAGS, LIBS: You can add additional libraries here, if needed.
    • MODULE: Your extension module name.
    • INPUT: The object files which make up your module.
    • Other variables: Probably shouldn't need modification.
  • Modify the .def file: Put in your module's name, give the file the correct name.
  • Cross fingers and run make -f mingw32-cross.mak


The information on this page has been successfully used with Python2.3 and Mingw32 api 2.3 / gcc 3.2.1. Your milage may vary.
Matthew Mueller,
Jan 16, 2010, 5:41 PM
Matthew Mueller,
Jan 16, 2010, 5:40 PM
Matthew Mueller,
Jan 16, 2010, 5:41 PM