3 Building Extensions: Tips and Tricks
Keywords: inst

Subsections

- 3.1 Using non-Microsoft compilers on Windows
  - 3.1.1 Borland C++
  - 3.1.2 GNU C / Cygwin / MinGW32

#
3 Building Extensions: Tips and Tricks

(This is the section to read for people doing any sort of interesting build. Things to talk about:

- the Setup file (any platform now, but Unix-biased)
- CFLAGS and LDFLAGS (must implement them first!)
- using non-MS compilers on Windows (how to convert Python's library, ...)

##
3.1 Using non-Microsoft compilers on Windows

### 3.1.1 Borland C++

This subsection describes the necessary steps to use Distutils with the Borland C++ compiler version 5.5.

First you have to know that the Borland's object file format(OMF) is different from what is used by the Python version you can download from the Python Web site. (Python is built with Microsoft Visual C++, which uses COFF as object file format.) For this reason you have to convert Python's library python20.lib into the Borland format. You can do this as follows:

      coff2omf python20.lib python20_bcpp.lib

The coff2omf program comes with the Borland compiler. The file python20.lib is in the Libs directory of your Python installation. If your extension uses other libraries (zlib,...) you have to convert them too.

The converted files have to reside in the same directories as the normal libraries.

How does Distutils manage to use these libraries with their changed names? If the extension needs a library (eg. foo) Distutils checks first if it finds a library with suffix _bcpp (eg. foo_bcpp.lib) and then uses this library. In the case it doesn't find such a special library it uses the default name (foo.lib.)1

To let Distutils compile your extension with Borland C++ you now have to type:

      python setup.py build --compiler=bcpp

If you want to use the Borland C++ compiler as default, you should consider to write it in your personal or system-wide configuration file for Distutils (see section 6.)

See Also:

C++Builder Compiler
  Information about the free C++ compiler from Borland, including links to the download pages.

Creating Python Extensions Using Borland's Free Compiler
  Document describing how to use Borland's free command-line C++ compiler to build Python.

### 3.1.2 GNU C / Cygwin / MinGW32

This section describes the necessary steps to use Distutils with the GNU C/C++ compilers in their Cygwin and MinGW32 distributions.2

** For a Python which was built with Cygwin, all should work without any of these following steps. **

For these compilers we have to create some special libraries too. This task is more complex as for Borland's C++, because there is no program to convert the library (inclusive the references on data structures.)

First you have to create a list of symbols which the Python DLL exports. (You can find a good program for this task at http://starship.python.net/crew/kernr/mingw32/Notes.html, see at PExports 0.42h there.)

      pexports python20.dll >python20.def

Then you can create from these information an import library for gcc.

      dlltool --dllname python20.dll --def python20.def --output-lib libpython20.a

The resulting library has to be placed in the same directory as python20.lib. (Should be the libs directory under your Python installation directory.)

If your extension uses other libraries (zlib,...) you might have to convert them too. The converted files have to reside in the same directories as the normal libraries do.

To let Distutils compile your extension with Cygwin you now have to type

      python setup.py build --compiler=cygwin

and for Cygwin in no-cygwin mode3 or for MinGW32 type:

      python setup.py build --compiler=mingw32

If you want to use any of these options/compilers as default, you should consider to write it in your personal or system-wide configuration file for Distutils (see section 6.)

See Also:

Building Python modules on MS Windows platform with MinGW32
  Information about building the required libraries for the MinGW32 environment.

http://pyopengl.sourceforge.net/ftp/win32-stuff/
  Converted import libraries in Cygwin/MinGW32 and Borland format, and a script to create the registry entries needed for Distutils to locate the built Python.

#### Footnotes

...foo.lib.)1
  This also means you could replace all existing COFF-libraries with OMF-libraries of the same name.
... distributions.2
  Check http://sources.redhat.com/cygwin/ and http://www.mingw.org/ for more information
... mode3
  Then you have no POSIX emulation available, but you also don't need cygwin1.dll.