#
3. Building C and C++ Extensions on Unix

Starting in Python 1.4, Python provides a special make file for building make files for building dynamically-linked extensions and custom interpreters. The make file make file builds a make file that reflects various system variables determined by configure when the Python interpreter was built, so people building module's don't have to resupply these settings. This vastly simplifies the process of building extensions and custom interpreters on Unix systems.

The make file make file is distributed as the file Misc/Makefile.pre.in in the Python source distribution. The first step in building extensions or custom interpreters is to copy this make file to a development directory containing extension module source.

The make file make file, Makefile.pre.in uses metadata provided in a file named Setup. The format of the Setup file is the same as the Setup (or Setup.dist) file provided in the Modules/ directory of the Python source distribution. The Setup file contains variable definitions:

      EC=/projects/ExtensionClass

and module description lines. It can also contain blank lines and comment lines that start with "`#`".

A module description line includes a module name, source files, options, variable references, and other input files, such as libraries or object files. Consider a simple example:

      ExtensionClass ExtensionClass.c

This is the simplest form of a module definition line. It defines a module, `ExtensionClass`, which has a single source file, ExtensionClass.c.

This slightly more complex example uses an -I option to specify an include directory:

      EC=/projects/ExtensionClass
      cPersistence cPersistence.c -I$(EC)

This example also illustrates the format for variable references.

For systems that support dynamic linking, the Setup file should begin:

      *shared*

to indicate that the modules defined in Setup are to be built as dynamically linked modules. A line containing only "`*static*`"can be used to indicate the subsequently listed modules should be statically linked.

Here is a complete Setup file for building a `cPersistent` module:

      # Set-up file to build the cPersistence module.
      # Note that the text should begin in the first column.
      *shared*

      # We need the path to the directory containing the ExtensionClass
      # include file.
      EC=/projects/ExtensionClass
      cPersistence cPersistence.c -I$(EC)

After the Setup file has been created, Makefile.pre.in is run with the "`boot`" target to create a make file:

      make -f Makefile.pre.in boot

This creates the file, Makefile. To build the extensions, simply run the created make file:

      make

It's not necessary to re-run Makefile.pre.in if the Setup file is changed. The make file automatically rebuilds itself if the Setup file changes.

Subsections

- 3.1 Building Custom Interpreters
- 3.2 Module Definition Options
- 3.3 Example
- 3.4 Distributing your extension modules