blob: 69d0cd99ba8943656eb570898230e51a16b6e55b [file] [log] [blame]
Fred Drake96511982002-04-05 19:54:19 +00001\chapter{Building C and \Cpp{} Extensions on Windows%
Fred Drakecc8f44b2001-08-20 19:30:29 +00002 \label{building-on-windows}}
3
4
5This chapter briefly explains how to create a Windows extension module
Fred Drakec37b65e2001-11-28 07:26:15 +00006for Python using Microsoft Visual \Cpp, and follows with more
Fred Drakecc8f44b2001-08-20 19:30:29 +00007detailed background information on how it works. The explanatory
8material is useful for both the Windows programmer learning to build
9Python extensions and the \UNIX{} programmer interested in producing
10software which can be successfully built on both \UNIX{} and Windows.
11
Martin v. Löwis27761f32002-03-09 10:06:14 +000012Module authors are encouraged to use the distutils approach for
13building extension modules, instead of the one described in this
14section. You will still need the C compiler that was used to build
15Python; typically Microsoft Visual \Cpp.
Fred Drakecc8f44b2001-08-20 19:30:29 +000016
Fred Drakec55ae4b2002-04-19 04:04:57 +000017\begin{notice}
18 This chapter mentions a number of filenames that include an encoded
19 Python version number. These filenames are represented with the
20 version number shown as \samp{XY}; in practive, \character{X} will
21 be the major version number and \character{Y} will be the minor
22 version number of the Python release you're working with. For
23 example, if you are using Python 2.2.1, \samp{XY} will actually be
24 \samp{22}.
25\end{notice}
26
27
Fred Drakecc8f44b2001-08-20 19:30:29 +000028\section{A Cookbook Approach \label{win-cookbook}}
29
Fred Drake5f293192001-12-13 17:20:32 +000030There are two approaches to building extension modules on Windows,
31just as there are on \UNIX: use the \refmodule{distutils} package to
32control the build process, or do things manually. The distutils
33approach works well for most extensions; documentation on using
34\refmodule{distutils} to build and package extension modules is
35available in \citetitle[../dist/dist.html]{Distributing Python
36Modules}. This section describes the manual approach to building
37Python extensions written in C or \Cpp.
Fred Drakecc8f44b2001-08-20 19:30:29 +000038
Fred Drake5f293192001-12-13 17:20:32 +000039To build extensions using these instructions, you need to have a copy
40of the Python sources of the same version as your installed Python.
41You will need Microsoft Visual \Cpp{} ``Developer Studio''; project
42files are supplied for V\Cpp{} version 6, but you can use older
43versions of V\Cpp. The example files described here are distributed
44with the Python sources in the \file{PC\textbackslash
45example_nt\textbackslash} directory.
Fred Drakecc8f44b2001-08-20 19:30:29 +000046
Fred Drake5f293192001-12-13 17:20:32 +000047\begin{enumerate}
48 \item
49 \strong{Copy the example files}\\
50 The \file{example_nt} directory is a subdirectory of the \file{PC}
51 directory, in order to keep all the PC-specific files under the
52 same directory in the source distribution. However, the
53 \file{example_nt} directory can't actually be used from this
54 location. You first need to copy or move it up one level, so that
55 \file{example_nt} is a sibling of the \file{PC} and \file{Include}
56 directories. Do all your work from within this new location.
Fred Drakecc8f44b2001-08-20 19:30:29 +000057
Fred Drake5f293192001-12-13 17:20:32 +000058 \item
59 \strong{Open the project}\\
60 From V\Cpp, use the \menuselection{File \sub Open Workspace}
61 dialog (not \menuselection{File \sub Open}!). Navigate to and
62 select the file \file{example.dsw}, in the \emph{copy} of the
63 \file{example_nt} directory you made above. Click Open.
Fred Drakecc8f44b2001-08-20 19:30:29 +000064
Fred Drake5f293192001-12-13 17:20:32 +000065 \item
66 \strong{Build the example DLL}\\
67 In order to check that everything is set up right, try building:
Fred Drakecc8f44b2001-08-20 19:30:29 +000068
Fred Drake5f293192001-12-13 17:20:32 +000069 \begin{enumerate}
70 \item
71 Select a configuration. This step is optional. Choose
72 \menuselection{Build \sub Select Active Configuration} and
73 select either ``example - Win32 Release'' or ``example - Win32
74 Debug.'' If you skip this step, V\Cpp{} will use the Debug
75 configuration by default.
Fred Drakecc8f44b2001-08-20 19:30:29 +000076
Fred Drake5f293192001-12-13 17:20:32 +000077 \item
78 Build the DLL. Choose \menuselection{Build \sub Build
79 example_d.dll} in Debug mode, or \menuselection{Build \sub
80 Build example.dll} in Release mode. This creates all
81 intermediate and result files in a subdirectory called either
82 \file{Debug} or \file{Release}, depending on which
83 configuration you selected in the preceding step.
84 \end{enumerate}
85
86 \item
87 \strong{Testing the debug-mode DLL}\\
88 Once the Debug build has succeeded, bring up a DOS box, and change
89 to the \file{example_nt\textbackslash Debug} directory. You
90 should now be able to repeat the following session (\code{C>} is
91 the DOS prompt, \code{>\code{>}>} is the Python prompt; note that
92 build information and various debug output from Python may not
93 match this screen dump exactly):
94
95\begin{verbatim}
96C>..\..\PCbuild\python_d
97Adding parser accelerators ...
98Done.
99Python 2.2 (#28, Dec 19 2001, 23:26:37) [MSC 32 bit (Intel)] on win32
100Type "copyright", "credits" or "license" for more information.
101>>> import example
102[4897 refs]
103>>> example.foo()
104Hello, world
105[4903 refs]
106>>>
107\end{verbatim}
108
109 Congratulations! You've successfully built your first Python
110 extension module.
111
112 \item
Thomas Heller291e9ee2002-07-04 08:36:53 +0000113 \strong{Creating your own project}\\
Fred Drake5f293192001-12-13 17:20:32 +0000114 Choose a name and create a directory for it. Copy your C sources
115 into it. Note that the module source file name does not
116 necessarily have to match the module name, but the name of the
117 initialization function should match the module name --- you can
118 only import a module \module{spam} if its initialization function
119 is called \cfunction{initspam()}, and it should call
120 \cfunction{Py_InitModule()} with the string \code{"spam"} as its
121 first argument (use the minimal \file{example.c} in this directory
122 as a guide). By convention, it lives in a file called
123 \file{spam.c} or \file{spammodule.c}. The output file should be
124 called \file{spam.dll} or \file{spam.pyd} (the latter is supported
125 to avoid confusion with a system library \file{spam.dll} to which
126 your module could be a Python interface) in Release mode, or
127 \file{spam_d.dll} or \file{spam_d.pyd} in Debug mode.
128
129 Now your options are:
130
131 \begin{enumerate}
132 \item Copy \file{example.dsw} and \file{example.dsp}, rename
133 them to \file{spam.*}, and edit them by hand, or
134 \item Create a brand new project; instructions are below.
135 \end{enumerate}
136
137 In either case, copy \file{example_nt\textbackslash example.def}
138 to \file{spam\textbackslash spam.def}, and edit the new
139 \file{spam.def} so its second line contains the string
140 `\code{initspam}'. If you created a new project yourself, add the
141 file \file{spam.def} to the project now. (This is an annoying
142 little file with only two lines. An alternative approach is to
143 forget about the \file{.def} file, and add the option
144 \programopt{/export:initspam} somewhere to the Link settings, by
145 manually editing the setting in Project Options dialog).
146
147 \item
148 \strong{Creating a brand new project}\\
149 Use the \menuselection{File \sub New \sub Projects} dialog to
150 create a new Project Workspace. Select ``Win32 Dynamic-Link
151 Library,'' enter the name (\samp{spam}), and make sure the
152 Location is set to the \file{spam} directory you have created
153 (which should be a direct subdirectory of the Python build tree, a
154 sibling of \file{Include} and \file{PC}). Select Win32 as the
155 platform (in my version, this is the only choice). Make sure the
156 Create new workspace radio button is selected. Click OK.
157
158 Now open the \menuselection{Project \sub Settings} dialog. You
159 only need to change a few settings. Make sure All Configurations
160 is selected from the Settings for: dropdown list. Select the
161 C/\Cpp{} tab. Choose the Preprocessor category in the popup menu
162 at the top. Type the following text in the entry box labeled
163 Addditional include directories:
164
165\begin{verbatim}
166..\Include,..\PC
167\end{verbatim}
168
169 Then, choose the Input category in the Link tab, and enter
170
171\begin{verbatim}
172..\PCbuild
173\end{verbatim}
174
175 in the text box labelled ``Additional library path.''
176
177 Now you need to add some mode-specific settings:
178
179 Select ``Win32 Release'' in the ``Settings for'' dropdown list.
180 Click the Link tab, choose the Input Category, and append
Fred Drakec55ae4b2002-04-19 04:04:57 +0000181 \code{pythonXY.lib} to the list in the ``Object/library modules''
Fred Drake5f293192001-12-13 17:20:32 +0000182 box.
183
184 Select ``Win32 Debug'' in the ``Settings for'' dropdown list, and
Fred Drakec55ae4b2002-04-19 04:04:57 +0000185 append \code{pythonXY_d.lib} to the list in the ``Object/library
Fred Drake5f293192001-12-13 17:20:32 +0000186 modules'' box. Then click the C/\Cpp{} tab, select ``Code
187 Generation'' from the Category dropdown list, and select ``Debug
188 Multithreaded DLL'' from the ``Use run-time library'' dropdown
189 list.
190
191 Select ``Win32 Release'' again from the ``Settings for'' dropdown
192 list. Select ``Multithreaded DLL'' from the ``Use run-time
193 library:'' dropdown list.
194
Fred Drakecc6cc5d2002-11-05 16:52:50 +0000195 You should now create the file \file{spam.def} as instructed in the
Fred Drake5f293192001-12-13 17:20:32 +0000196 previous section. Then chose the \menuselection{Insert \sub Files
197 into Project} dialog. Set the pattern to \code{*.*} and select
198 both \file{spam.c} and \file{spam.def} and click OK. (Inserting
199 them one by one is fine too.)
200\end{enumerate}
201
Fred Drakecc8f44b2001-08-20 19:30:29 +0000202
203If your module creates a new type, you may have trouble with this line:
204
205\begin{verbatim}
206 PyObject_HEAD_INIT(&PyType_Type)
207\end{verbatim}
208
209Change it to:
210
211\begin{verbatim}
212 PyObject_HEAD_INIT(NULL)
213\end{verbatim}
214
215and add the following to the module initialization function:
216
217\begin{verbatim}
218 MyObject_Type.ob_type = &PyType_Type;
219\end{verbatim}
220
Fred Drakecc6cc5d2002-11-05 16:52:50 +0000221Refer to section~3 of the
Fred Drakecc8f44b2001-08-20 19:30:29 +0000222\citetitle[http://www.python.org/doc/FAQ.html]{Python FAQ} for details
223on why you must do this.
224
225
226\section{Differences Between \UNIX{} and Windows
227 \label{dynamic-linking}}
228\sectionauthor{Chris Phoenix}{cphoenix@best.com}
229
230
231\UNIX{} and Windows use completely different paradigms for run-time
232loading of code. Before you try to build a module that can be
233dynamically loaded, be aware of how your system works.
234
Fred Drakec37b65e2001-11-28 07:26:15 +0000235In \UNIX, a shared object (\file{.so}) file contains code to be used by the
Fred Drakecc8f44b2001-08-20 19:30:29 +0000236program, and also the names of functions and data that it expects to
237find in the program. When the file is joined to the program, all
238references to those functions and data in the file's code are changed
239to point to the actual locations in the program where the functions
240and data are placed in memory. This is basically a link operation.
241
242In Windows, a dynamic-link library (\file{.dll}) file has no dangling
243references. Instead, an access to functions or data goes through a
244lookup table. So the DLL code does not have to be fixed up at runtime
245to refer to the program's memory; instead, the code already uses the
246DLL's lookup table, and the lookup table is modified at runtime to
247point to the functions and data.
248
Fred Drakec37b65e2001-11-28 07:26:15 +0000249In \UNIX, there is only one type of library file (\file{.a}) which
Fred Drakecc8f44b2001-08-20 19:30:29 +0000250contains code from several object files (\file{.o}). During the link
251step to create a shared object file (\file{.so}), the linker may find
252that it doesn't know where an identifier is defined. The linker will
253look for it in the object files in the libraries; if it finds it, it
254will include all the code from that object file.
255
256In Windows, there are two types of library, a static library and an
257import library (both called \file{.lib}). A static library is like a
258\UNIX{} \file{.a} file; it contains code to be included as necessary.
259An import library is basically used only to reassure the linker that a
260certain identifier is legal, and will be present in the program when
261the DLL is loaded. So the linker uses the information from the
262import library to build the lookup table for using identifiers that
263are not included in the DLL. When an application or a DLL is linked,
264an import library may be generated, which will need to be used for all
265future DLLs that depend on the symbols in the application or DLL.
266
267Suppose you are building two dynamic-load modules, B and C, which should
Fred Drakec37b65e2001-11-28 07:26:15 +0000268share another block of code A. On \UNIX, you would \emph{not} pass
Fred Drakecc8f44b2001-08-20 19:30:29 +0000269\file{A.a} to the linker for \file{B.so} and \file{C.so}; that would
270cause it to be included twice, so that B and C would each have their
271own copy. In Windows, building \file{A.dll} will also build
272\file{A.lib}. You \emph{do} pass \file{A.lib} to the linker for B and
273C. \file{A.lib} does not contain code; it just contains information
274which will be used at runtime to access A's code.
275
276In Windows, using an import library is sort of like using \samp{import
277spam}; it gives you access to spam's names, but does not create a
Fred Drakec37b65e2001-11-28 07:26:15 +0000278separate copy. On \UNIX, linking with a library is more like
Fred Drakecc8f44b2001-08-20 19:30:29 +0000279\samp{from spam import *}; it does create a separate copy.
280
281
282\section{Using DLLs in Practice \label{win-dlls}}
283\sectionauthor{Chris Phoenix}{cphoenix@best.com}
284
Fred Drakec37b65e2001-11-28 07:26:15 +0000285Windows Python is built in Microsoft Visual \Cpp; using other
Fred Drakecc8f44b2001-08-20 19:30:29 +0000286compilers may or may not work (though Borland seems to). The rest of
287this section is MSV\Cpp{} specific.
288
Fred Drakec55ae4b2002-04-19 04:04:57 +0000289When creating DLLs in Windows, you must pass \file{pythonXY.lib} to
Fred Drakecc8f44b2001-08-20 19:30:29 +0000290the linker. To build two DLLs, spam and ni (which uses C functions
291found in spam), you could use these commands:
292
293\begin{verbatim}
Fred Drakec55ae4b2002-04-19 04:04:57 +0000294cl /LD /I/python/include spam.c ../libs/pythonXY.lib
295cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib
Fred Drakecc8f44b2001-08-20 19:30:29 +0000296\end{verbatim}
297
298The first command created three files: \file{spam.obj},
299\file{spam.dll} and \file{spam.lib}. \file{Spam.dll} does not contain
300any Python functions (such as \cfunction{PyArg_ParseTuple()}), but it
Fred Drakec55ae4b2002-04-19 04:04:57 +0000301does know how to find the Python code thanks to \file{pythonXY.lib}.
Fred Drakecc8f44b2001-08-20 19:30:29 +0000302
303The second command created \file{ni.dll} (and \file{.obj} and
304\file{.lib}), which knows how to find the necessary functions from
305spam, and also from the Python executable.
306
307Not every identifier is exported to the lookup table. If you want any
308other modules (including Python) to be able to see your identifiers,
309you have to say \samp{_declspec(dllexport)}, as in \samp{void
310_declspec(dllexport) initspam(void)} or \samp{PyObject
311_declspec(dllexport) *NiGetSpamData(void)}.
312
313Developer Studio will throw in a lot of import libraries that you do
314not really need, adding about 100K to your executable. To get rid of
315them, use the Project Settings dialog, Link tab, to specify
316\emph{ignore default libraries}. Add the correct
317\file{msvcrt\var{xx}.lib} to the list of libraries.