<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>2. Building C and C++ Extensions on Unix
</TITLE>
<META NAME="description" CONTENT="2. Building C and C++ Extensions on Unix
">
<META NAME="keywords" CONTENT="ext">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="ext.css">
<LINK REL="next" HREF="building-on-windows.html">
<LINK REL="previous" HREF="intro.html">
<LINK REL="up" HREF="ext.html">
<LINK REL="next" HREF="custom-interps.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="custom-interps.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="ext.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="using-cobjects.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Extending and Embedding the Python Interpreter</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="custom-interps.html">2.1 Building Custom Interpreters</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="ext.html">Extending and Embedding the</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="using-cobjects.html">1.12 Providing a C</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION004000000000000000000"></A>
<BR>
2. Building C and C++ Extensions on Unix
         
</H1>

<P>

<P>
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.

<P>
The make file make file is distributed as the file
<tt class=file>Misc/Makefile.pre.in</tt> 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.

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

<P>
<dl><dd><pre class=verbatim>
EC=/projects/ExtensionClass
</pre></dl>

<P>
and module description lines.  It can also contain blank lines and
comment lines that start with "<tt class=character>#</tt>".

<P>
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::

<P>
<dl><dd><pre class=verbatim>
ExtensionClass ExtensionClass.c
</pre></dl>

<P>
This is the simplest form of a module definition line.  It defines a
module, <tt class=module>ExtensionClass</tt>, which has a single source file,
<tt class=file>ExtensionClass.c</tt>.

<P>
This slightly more complex example uses an <b>-I</b> option to
specify an include directory:

<P>
<dl><dd><pre class=verbatim>
EC=/projects/ExtensionClass
cPersistence cPersistence.c -I$(EC)
</pre></dl> 
<P>
This example also illustrates the format for variable references.

<P>
For systems that support dynamic linking, the <tt class=file>Setup</tt> file should 
begin:

<P>
<dl><dd><pre class=verbatim>
*shared*
</pre></dl>

<P>
to indicate that the modules defined in <tt class=file>Setup</tt> are to be built
as dynamically linked modules.  A line containing only "<tt class=samp>*static*</tt>"can be used to indicate the subsequently listed modules should be
statically linked.

<P>
Here is a complete <tt class=file>Setup</tt> file for building a
<tt class=module>cPersistent</tt> module:

<P>
<dl><dd><pre class=verbatim>
# 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)
</pre></dl> 
<P>
After the <tt class=file>Setup</tt> file has been created, <tt class=file>Makefile.pre.in</tt>
is run with the "<tt class=samp>boot</tt>" target to create a make file:

<P>
<dl><dd><pre class=verbatim>
make -f Makefile.pre.in boot
</pre></dl>

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

<P>
<dl><dd><pre class=verbatim>
make
</pre></dl>

<P>
It's not necessary to re-run <tt class=file>Makefile.pre.in</tt> if the
<tt class=file>Setup</tt> file is changed.  The make file automatically rebuilds
itself if the <tt class=file>Setup</tt> file changes.

<P>

<p><hr>
<!--Table of Child-Links-->


<UL>
<LI><A NAME="tex2html301"
 HREF="custom-interps.html">2.1 Building Custom Interpreters </A>
<LI><A NAME="tex2html302"
 HREF="module-defn-options.html">2.2 Module Definition Options </A>
<LI><A NAME="tex2html303"
 HREF="module-defn-example.html">2.3 Example </A>
<LI><A NAME="tex2html304"
 HREF="distributing.html">2.4 Distributing your extension modules
         </A>
</UL>
<!--End of Table of Child-Links-->
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="custom-interps.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="ext.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="using-cobjects.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Extending and Embedding the Python Interpreter</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="custom-interps.html">2.1 Building Custom Interpreters</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="ext.html">Extending and Embedding the</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="using-cobjects.html">1.12 Providing a C</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
