Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Doc / ext / windows.tex
blob69d0cd99ba8943656eb570898230e51a16b6e55b
1 \chapter{Building C and \Cpp{} Extensions on Windows%
2 \label{building-on-windows}}
5 This chapter briefly explains how to create a Windows extension module
6 for Python using Microsoft Visual \Cpp, and follows with more
7 detailed background information on how it works. The explanatory
8 material is useful for both the Windows programmer learning to build
9 Python extensions and the \UNIX{} programmer interested in producing
10 software which can be successfully built on both \UNIX{} and Windows.
12 Module authors are encouraged to use the distutils approach for
13 building extension modules, instead of the one described in this
14 section. You will still need the C compiler that was used to build
15 Python; typically Microsoft Visual \Cpp.
17 \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}
28 \section{A Cookbook Approach \label{win-cookbook}}
30 There are two approaches to building extension modules on Windows,
31 just as there are on \UNIX: use the \refmodule{distutils} package to
32 control the build process, or do things manually. The distutils
33 approach works well for most extensions; documentation on using
34 \refmodule{distutils} to build and package extension modules is
35 available in \citetitle[../dist/dist.html]{Distributing Python
36 Modules}. This section describes the manual approach to building
37 Python extensions written in C or \Cpp.
39 To build extensions using these instructions, you need to have a copy
40 of the Python sources of the same version as your installed Python.
41 You will need Microsoft Visual \Cpp{} ``Developer Studio''; project
42 files are supplied for V\Cpp{} version 6, but you can use older
43 versions of V\Cpp. The example files described here are distributed
44 with the Python sources in the \file{PC\textbackslash
45 example_nt\textbackslash} directory.
47 \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.
58 \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.
65 \item
66 \strong{Build the example DLL}\\
67 In order to check that everything is set up right, try building:
69 \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.
77 \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}
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):
95 \begin{verbatim}
96 C>..\..\PCbuild\python_d
97 Adding parser accelerators ...
98 Done.
99 Python 2.2 (#28, Dec 19 2001, 23:26:37) [MSC 32 bit (Intel)] on win32
100 Type "copyright", "credits" or "license" for more information.
101 >>> import example
102 [4897 refs]
103 >>> example.foo()
104 Hello, world
105 [4903 refs]
107 \end{verbatim}
109 Congratulations! You've successfully built your first Python
110 extension module.
112 \item
113 \strong{Creating your own project}\\
114 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.
129 Now your options are:
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}
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).
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.
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:
165 \begin{verbatim}
166 ..\Include,..\PC
167 \end{verbatim}
169 Then, choose the Input category in the Link tab, and enter
171 \begin{verbatim}
172 ..\PCbuild
173 \end{verbatim}
175 in the text box labelled ``Additional library path.''
177 Now you need to add some mode-specific settings:
179 Select ``Win32 Release'' in the ``Settings for'' dropdown list.
180 Click the Link tab, choose the Input Category, and append
181 \code{pythonXY.lib} to the list in the ``Object/library modules''
182 box.
184 Select ``Win32 Debug'' in the ``Settings for'' dropdown list, and
185 append \code{pythonXY_d.lib} to the list in the ``Object/library
186 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.
191 Select ``Win32 Release'' again from the ``Settings for'' dropdown
192 list. Select ``Multithreaded DLL'' from the ``Use run-time
193 library:'' dropdown list.
195 You should now create the file \file{spam.def} as instructed in the
196 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}
203 If your module creates a new type, you may have trouble with this line:
205 \begin{verbatim}
206 PyObject_HEAD_INIT(&PyType_Type)
207 \end{verbatim}
209 Change it to:
211 \begin{verbatim}
212 PyObject_HEAD_INIT(NULL)
213 \end{verbatim}
215 and add the following to the module initialization function:
217 \begin{verbatim}
218 MyObject_Type.ob_type = &PyType_Type;
219 \end{verbatim}
221 Refer to section~3 of the
222 \citetitle[http://www.python.org/doc/FAQ.html]{Python FAQ} for details
223 on why you must do this.
226 \section{Differences Between \UNIX{} and Windows
227 \label{dynamic-linking}}
228 \sectionauthor{Chris Phoenix}{cphoenix@best.com}
231 \UNIX{} and Windows use completely different paradigms for run-time
232 loading of code. Before you try to build a module that can be
233 dynamically loaded, be aware of how your system works.
235 In \UNIX, a shared object (\file{.so}) file contains code to be used by the
236 program, and also the names of functions and data that it expects to
237 find in the program. When the file is joined to the program, all
238 references to those functions and data in the file's code are changed
239 to point to the actual locations in the program where the functions
240 and data are placed in memory. This is basically a link operation.
242 In Windows, a dynamic-link library (\file{.dll}) file has no dangling
243 references. Instead, an access to functions or data goes through a
244 lookup table. So the DLL code does not have to be fixed up at runtime
245 to refer to the program's memory; instead, the code already uses the
246 DLL's lookup table, and the lookup table is modified at runtime to
247 point to the functions and data.
249 In \UNIX, there is only one type of library file (\file{.a}) which
250 contains code from several object files (\file{.o}). During the link
251 step to create a shared object file (\file{.so}), the linker may find
252 that it doesn't know where an identifier is defined. The linker will
253 look for it in the object files in the libraries; if it finds it, it
254 will include all the code from that object file.
256 In Windows, there are two types of library, a static library and an
257 import library (both called \file{.lib}). A static library is like a
258 \UNIX{} \file{.a} file; it contains code to be included as necessary.
259 An import library is basically used only to reassure the linker that a
260 certain identifier is legal, and will be present in the program when
261 the DLL is loaded. So the linker uses the information from the
262 import library to build the lookup table for using identifiers that
263 are not included in the DLL. When an application or a DLL is linked,
264 an import library may be generated, which will need to be used for all
265 future DLLs that depend on the symbols in the application or DLL.
267 Suppose you are building two dynamic-load modules, B and C, which should
268 share another block of code A. On \UNIX, you would \emph{not} pass
269 \file{A.a} to the linker for \file{B.so} and \file{C.so}; that would
270 cause it to be included twice, so that B and C would each have their
271 own 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
273 C. \file{A.lib} does not contain code; it just contains information
274 which will be used at runtime to access A's code.
276 In Windows, using an import library is sort of like using \samp{import
277 spam}; it gives you access to spam's names, but does not create a
278 separate copy. On \UNIX, linking with a library is more like
279 \samp{from spam import *}; it does create a separate copy.
282 \section{Using DLLs in Practice \label{win-dlls}}
283 \sectionauthor{Chris Phoenix}{cphoenix@best.com}
285 Windows Python is built in Microsoft Visual \Cpp; using other
286 compilers may or may not work (though Borland seems to). The rest of
287 this section is MSV\Cpp{} specific.
289 When creating DLLs in Windows, you must pass \file{pythonXY.lib} to
290 the linker. To build two DLLs, spam and ni (which uses C functions
291 found in spam), you could use these commands:
293 \begin{verbatim}
294 cl /LD /I/python/include spam.c ../libs/pythonXY.lib
295 cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib
296 \end{verbatim}
298 The first command created three files: \file{spam.obj},
299 \file{spam.dll} and \file{spam.lib}. \file{Spam.dll} does not contain
300 any Python functions (such as \cfunction{PyArg_ParseTuple()}), but it
301 does know how to find the Python code thanks to \file{pythonXY.lib}.
303 The second command created \file{ni.dll} (and \file{.obj} and
304 \file{.lib}), which knows how to find the necessary functions from
305 spam, and also from the Python executable.
307 Not every identifier is exported to the lookup table. If you want any
308 other modules (including Python) to be able to see your identifiers,
309 you have to say \samp{_declspec(dllexport)}, as in \samp{void
310 _declspec(dllexport) initspam(void)} or \samp{PyObject
311 _declspec(dllexport) *NiGetSpamData(void)}.
313 Developer Studio will throw in a lot of import libraries that you do
314 not really need, adding about 100K to your executable. To get rid of
315 them, 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.