blob: 7e6dfd2f2e949457f92eadcc58f1a4d79ec9f2af [file] [log] [blame]
Fred Drakecc8f44b2001-08-20 19:30:29 +00001\chapter{Building C and \Cpp{} Extensions on \UNIX{}
2 \label{building-on-unix}}
3
4\sectionauthor{Jim Fulton}{jim@zope.com}
5
6
7%The make file make file, building C extensions on Unix
8
9
10Starting in Python 1.4, Python provides a special make file for
11building make files for building dynamically-linked extensions and
12custom interpreters. The make file make file builds a make file
13that reflects various system variables determined by configure when
14the Python interpreter was built, so people building module's don't
15have to resupply these settings. This vastly simplifies the process
16of building extensions and custom interpreters on Unix systems.
17
18The make file make file is distributed as the file
19\file{Misc/Makefile.pre.in} in the Python source distribution. The
20first step in building extensions or custom interpreters is to copy
21this make file to a development directory containing extension module
22source.
23
24The make file make file, \file{Makefile.pre.in} uses metadata
25provided in a file named \file{Setup}. The format of the \file{Setup}
26file is the same as the \file{Setup} (or \file{Setup.dist}) file
27provided in the \file{Modules/} directory of the Python source
28distribution. The \file{Setup} file contains variable definitions:
29
30\begin{verbatim}
31EC=/projects/ExtensionClass
32\end{verbatim}
33
34and module description lines. It can also contain blank lines and
35comment lines that start with \character{\#}.
36
37A module description line includes a module name, source files,
38options, variable references, and other input files, such
39as libraries or object files. Consider a simple example:
40
41\begin{verbatim}
42ExtensionClass ExtensionClass.c
43\end{verbatim}
44
45This is the simplest form of a module definition line. It defines a
46module, \module{ExtensionClass}, which has a single source file,
47\file{ExtensionClass.c}.
48
49This slightly more complex example uses an \strong{-I} option to
50specify an include directory:
51
52\begin{verbatim}
53EC=/projects/ExtensionClass
54cPersistence cPersistence.c -I$(EC)
55\end{verbatim} % $ <-- bow to font lock
56
57This example also illustrates the format for variable references.
58
59For systems that support dynamic linking, the \file{Setup} file should
60begin:
61
62\begin{verbatim}
63*shared*
64\end{verbatim}
65
66to indicate that the modules defined in \file{Setup} are to be built
67as dynamically linked modules. A line containing only \samp{*static*}
68can be used to indicate the subsequently listed modules should be
69statically linked.
70
71Here is a complete \file{Setup} file for building a
72\module{cPersistent} module:
73
74\begin{verbatim}
75# Set-up file to build the cPersistence module.
76# Note that the text should begin in the first column.
77*shared*
78
79# We need the path to the directory containing the ExtensionClass
80# include file.
81EC=/projects/ExtensionClass
82cPersistence cPersistence.c -I$(EC)
83\end{verbatim} % $ <-- bow to font lock
84
85After the \file{Setup} file has been created, \file{Makefile.pre.in}
86is run with the \samp{boot} target to create a make file:
87
88\begin{verbatim}
89make -f Makefile.pre.in boot
90\end{verbatim}
91
92This creates the file, Makefile. To build the extensions, simply
93run the created make file:
94
95\begin{verbatim}
96make
97\end{verbatim}
98
99It's not necessary to re-run \file{Makefile.pre.in} if the
100\file{Setup} file is changed. The make file automatically rebuilds
101itself if the \file{Setup} file changes.
102
103
104\section{Building Custom Interpreters \label{custom-interps}}
105
106The make file built by \file{Makefile.pre.in} can be run with the
107\samp{static} target to build an interpreter:
108
109\begin{verbatim}
110make static
111\end{verbatim}
112
113Any modules defined in the \file{Setup} file before the
114\samp{*shared*} line will be statically linked into the interpreter.
115Typically, a \samp{*shared*} line is omitted from the
116\file{Setup} file when a custom interpreter is desired.
117
118
119\section{Module Definition Options \label{module-defn-options}}
120
121Several compiler options are supported:
122
123\begin{tableii}{l|l}{programopt}{Option}{Meaning}
124 \lineii{-C}{Tell the C pre-processor not to discard comments}
125 \lineii{-D\var{name}=\var{value}}{Define a macro}
126 \lineii{-I\var{dir}}{Specify an include directory, \var{dir}}
127 \lineii{-L\var{dir}}{Specify a link-time library directory, \var{dir}}
128 \lineii{-R\var{dir}}{Specify a run-time library directory, \var{dir}}
129 \lineii{-l\var{lib}}{Link a library, \var{lib}}
130 \lineii{-U\var{name}}{Undefine a macro}
131\end{tableii}
132
133Other compiler options can be included (snuck in) by putting them
134in variables.
135
136Source files can include files with \file{.c}, \file{.C}, \file{.cc},
137\file{.cpp}, \file{.cxx}, and \file{.c++} extensions.
138
139Other input files include files with \file{.a}, \file{.o}, \file{.sl},
140and \file{.so} extensions.
141
142
143\section{Example \label{module-defn-example}}
144
145Here is a more complicated example from \file{Modules/Setup.dist}:
146
147\begin{verbatim}
148GMP=/ufs/guido/src/gmp
149mpz mpzmodule.c -I$(GMP) $(GMP)/libgmp.a
150\end{verbatim}
151
152which could also be written as:
153
154\begin{verbatim}
155mpz mpzmodule.c -I$(GMP) -L$(GMP) -lgmp
156\end{verbatim}
157
158
159\section{Distributing your extension modules
160 \label{distributing}}
161
162There are two ways to distribute extension modules for others to use.
163The way that allows the easiest cross-platform support is to use the
164\module{distutils}\refstmodindex{distutils} package. The manual
165\citetitle[../dist/dist.html]{Distributing Python Modules} contains
166information on this approach. It is recommended that all new
167extensions be distributed using this approach to allow easy building
168and installation across platforms. Older extensions should migrate to
169this approach as well.
170
171What follows describes the older approach; there are still many
172extensions which use this.
173
174When distributing your extension modules in source form, make sure to
175include a \file{Setup} file. The \file{Setup} file should be named
176\file{Setup.in} in the distribution. The make file make file,
177\file{Makefile.pre.in}, will copy \file{Setup.in} to \file{Setup} if
178the person installing the extension doesn't do so manually.
179Distributing a \file{Setup.in} file makes it easy for people to
180customize the \file{Setup} file while keeping the original in
181\file{Setup.in}.
182
183It is a good idea to include a copy of \file{Makefile.pre.in} for
184people who do not have a source distribution of Python.
185
186Do not distribute a make file. People building your modules
187should use \file{Makefile.pre.in} to build their own make file. A
188\file{README} file included in the package should provide simple
189instructions to perform the build.