blob: ca18a1ec94aee6bb208805f87c04201e06c1e439 [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
Martin v. Löwis658b50f2004-12-30 10:44:32 +000042files are supplied for V\Cpp{} version 7.1, but you can use older
43versions of V\Cpp. Notice that you should use the same version of
44V\Cpp that was used to build Python itself. The example files
45described here are distributed with the Python sources in the
46\file{PC\textbackslash example_nt\textbackslash} directory.
Fred Drakecc8f44b2001-08-20 19:30:29 +000047
Fred Drake5f293192001-12-13 17:20:32 +000048\begin{enumerate}
49 \item
50 \strong{Copy the example files}\\
51 The \file{example_nt} directory is a subdirectory of the \file{PC}
52 directory, in order to keep all the PC-specific files under the
53 same directory in the source distribution. However, the
54 \file{example_nt} directory can't actually be used from this
55 location. You first need to copy or move it up one level, so that
56 \file{example_nt} is a sibling of the \file{PC} and \file{Include}
57 directories. Do all your work from within this new location.
Fred Drakecc8f44b2001-08-20 19:30:29 +000058
Fred Drake5f293192001-12-13 17:20:32 +000059 \item
60 \strong{Open the project}\\
Martin v. Löwis658b50f2004-12-30 10:44:32 +000061 From V\Cpp, use the \menuselection{File \sub Open Solution}
Fred Drake5f293192001-12-13 17:20:32 +000062 dialog (not \menuselection{File \sub Open}!). Navigate to and
Martin v. Löwis658b50f2004-12-30 10:44:32 +000063 select the file \file{example.sln}, in the \emph{copy} of the
Fred Drake5f293192001-12-13 17:20:32 +000064 \file{example_nt} directory you made above. Click Open.
Fred Drakecc8f44b2001-08-20 19:30:29 +000065
Fred Drake5f293192001-12-13 17:20:32 +000066 \item
67 \strong{Build the example DLL}\\
68 In order to check that everything is set up right, try building:
Fred Drakecc8f44b2001-08-20 19:30:29 +000069
Fred Drake5f293192001-12-13 17:20:32 +000070 \begin{enumerate}
71 \item
72 Select a configuration. This step is optional. Choose
Martin v. Löwis658b50f2004-12-30 10:44:32 +000073 \menuselection{Build \sub Configuration Manager \sub Active
74 Solution Configuration} and select either \guilabel{Release}
75 or\guilabel{Debug}. If you skip this step,
Fred Drake0d8da3a2004-01-23 09:01:56 +000076 V\Cpp{} will use the Debug configuration by default.
Fred Drakecc8f44b2001-08-20 19:30:29 +000077
Fred Drake5f293192001-12-13 17:20:32 +000078 \item
79 Build the DLL. Choose \menuselection{Build \sub Build
Martin v. Löwis658b50f2004-12-30 10:44:32 +000080 Solution}. This creates all intermediate and result files in
81 a subdirectory called either \file{Debug} or \file{Release},
82 depending on which configuration you selected in the preceding
83 step.
Fred Drake5f293192001-12-13 17:20:32 +000084 \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
Thomas Wouters477c8d52006-05-27 19:21:47 +000091 the DOS prompt, \code{>>>} is the Python prompt; note that
Fred Drake5f293192001-12-13 17:20:32 +000092 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}
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000132 \item Copy \file{example.sln} and \file{example.vcproj}, rename
Fred Drake5f293192001-12-13 17:20:32 +0000133 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
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000145 manually editing the setting in Project Properties dialog).
Fred Drake5f293192001-12-13 17:20:32 +0000146
147 \item
148 \strong{Creating a brand new project}\\
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000149 Use the \menuselection{File \sub New \sub Project} dialog to
150 create a new Project Workspace. Select \guilabel{Visual C++
151 Projects/Win32/ Win32 Project}, enter the name (\samp{spam}), and
152 make sure the Location is set to parent of the \file{spam}
153 directory you have created (which should be a direct subdirectory
154 of the Python build tree, a sibling of \file{Include} and
155 \file{PC}). Select Win32 as the platform (in my version, this is
156 the only choice). Make sure the Create new workspace radio button
157 is selected. Click OK.
Fred Drake5f293192001-12-13 17:20:32 +0000158
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000159 You should now create the file \file{spam.def} as instructed in
160 the previous section. Add the source files to the project, using
161 \menuselection{Project \sub Add Existing Item}. Set the pattern to
162 \code{*.*} and select both \file{spam.c} and \file{spam.def} and
163 click OK. (Inserting them one by one is fine too.)
164
165 Now open the \menuselection{Project \sub spam properties} dialog.
Martin v. Löwisd61788b2005-01-03 23:42:01 +0000166 You only need to change a few settings. Make sure \guilabel{All
167 Configurations} is selected from the \guilabel{Settings for:}
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000168 dropdown list. Select the C/\Cpp{} tab. Choose the General
169 category in the popup menu at the top. Type the following text in
170 the entry box labeled \guilabel{Additional Include Directories}:
Fred Drake5f293192001-12-13 17:20:32 +0000171
172\begin{verbatim}
173..\Include,..\PC
174\end{verbatim}
175
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000176 Then, choose the General category in the Linker tab, and enter
Fred Drake5f293192001-12-13 17:20:32 +0000177
178\begin{verbatim}
179..\PCbuild
180\end{verbatim}
181
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000182 in the text box labelled \guilabel{Additional library Directories}.
Fred Drake5f293192001-12-13 17:20:32 +0000183
184 Now you need to add some mode-specific settings:
185
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000186 Select \guilabel{Release} in the \guilabel{Configuration}
Fred Drake0d8da3a2004-01-23 09:01:56 +0000187 dropdown list. Choose the \guilabel{Link} tab, choose the
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000188 \guilabel{Input} category, and append \code{pythonXY.lib} to the
189 list in the \guilabel{Additional Dependencies} box.
Fred Drake5f293192001-12-13 17:20:32 +0000190
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000191 Select \guilabel{Debug} in the \guilabel{Configuration} dropdown
192 list, and append \code{pythonXY_d.lib} to the list in the
193 \guilabel{Additional Dependencies} box. Then click the C/\Cpp{}
194 tab, select \guilabel{Code Generation}, and select
195 \guilabel{Multi-threaded Debug DLL} from the \guilabel{Runtime
196 library} dropdown list.
Fred Drake5f293192001-12-13 17:20:32 +0000197
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000198 Select \guilabel{Release} again from the \guilabel{Configuration}
199 dropdown list. Select \guilabel{Multi-threaded DLL} from the
200 \guilabel{Runtime library} dropdown list.
Fred Drake5f293192001-12-13 17:20:32 +0000201\end{enumerate}
202
Fred Drakecc8f44b2001-08-20 19:30:29 +0000203
204If your module creates a new type, you may have trouble with this line:
205
206\begin{verbatim}
207 PyObject_HEAD_INIT(&PyType_Type)
208\end{verbatim}
209
210Change it to:
211
212\begin{verbatim}
213 PyObject_HEAD_INIT(NULL)
214\end{verbatim}
215
216and add the following to the module initialization function:
217
218\begin{verbatim}
219 MyObject_Type.ob_type = &PyType_Type;
220\end{verbatim}
221
Fred Drakecc6cc5d2002-11-05 16:52:50 +0000222Refer to section~3 of the
Fred Drakecc8f44b2001-08-20 19:30:29 +0000223\citetitle[http://www.python.org/doc/FAQ.html]{Python FAQ} for details
224on why you must do this.
225
226
227\section{Differences Between \UNIX{} and Windows
228 \label{dynamic-linking}}
229\sectionauthor{Chris Phoenix}{cphoenix@best.com}
230
231
232\UNIX{} and Windows use completely different paradigms for run-time
233loading of code. Before you try to build a module that can be
234dynamically loaded, be aware of how your system works.
235
Fred Drakec37b65e2001-11-28 07:26:15 +0000236In \UNIX, a shared object (\file{.so}) file contains code to be used by the
Fred Drakecc8f44b2001-08-20 19:30:29 +0000237program, and also the names of functions and data that it expects to
238find in the program. When the file is joined to the program, all
239references to those functions and data in the file's code are changed
240to point to the actual locations in the program where the functions
241and data are placed in memory. This is basically a link operation.
242
243In Windows, a dynamic-link library (\file{.dll}) file has no dangling
244references. Instead, an access to functions or data goes through a
245lookup table. So the DLL code does not have to be fixed up at runtime
246to refer to the program's memory; instead, the code already uses the
247DLL's lookup table, and the lookup table is modified at runtime to
248point to the functions and data.
249
Fred Drakec37b65e2001-11-28 07:26:15 +0000250In \UNIX, there is only one type of library file (\file{.a}) which
Fred Drakecc8f44b2001-08-20 19:30:29 +0000251contains code from several object files (\file{.o}). During the link
252step to create a shared object file (\file{.so}), the linker may find
253that it doesn't know where an identifier is defined. The linker will
254look for it in the object files in the libraries; if it finds it, it
255will include all the code from that object file.
256
257In Windows, there are two types of library, a static library and an
258import library (both called \file{.lib}). A static library is like a
259\UNIX{} \file{.a} file; it contains code to be included as necessary.
260An import library is basically used only to reassure the linker that a
261certain identifier is legal, and will be present in the program when
262the DLL is loaded. So the linker uses the information from the
263import library to build the lookup table for using identifiers that
264are not included in the DLL. When an application or a DLL is linked,
265an import library may be generated, which will need to be used for all
266future DLLs that depend on the symbols in the application or DLL.
267
268Suppose you are building two dynamic-load modules, B and C, which should
Fred Drakec37b65e2001-11-28 07:26:15 +0000269share another block of code A. On \UNIX, you would \emph{not} pass
Fred Drakecc8f44b2001-08-20 19:30:29 +0000270\file{A.a} to the linker for \file{B.so} and \file{C.so}; that would
271cause it to be included twice, so that B and C would each have their
272own copy. In Windows, building \file{A.dll} will also build
273\file{A.lib}. You \emph{do} pass \file{A.lib} to the linker for B and
274C. \file{A.lib} does not contain code; it just contains information
275which will be used at runtime to access A's code.
276
277In Windows, using an import library is sort of like using \samp{import
278spam}; it gives you access to spam's names, but does not create a
Fred Drakec37b65e2001-11-28 07:26:15 +0000279separate copy. On \UNIX, linking with a library is more like
Fred Drakecc8f44b2001-08-20 19:30:29 +0000280\samp{from spam import *}; it does create a separate copy.
281
282
283\section{Using DLLs in Practice \label{win-dlls}}
284\sectionauthor{Chris Phoenix}{cphoenix@best.com}
285
Fred Drakec37b65e2001-11-28 07:26:15 +0000286Windows Python is built in Microsoft Visual \Cpp; using other
Fred Drakecc8f44b2001-08-20 19:30:29 +0000287compilers may or may not work (though Borland seems to). The rest of
288this section is MSV\Cpp{} specific.
289
Fred Drakec55ae4b2002-04-19 04:04:57 +0000290When creating DLLs in Windows, you must pass \file{pythonXY.lib} to
Fred Drakecc8f44b2001-08-20 19:30:29 +0000291the linker. To build two DLLs, spam and ni (which uses C functions
292found in spam), you could use these commands:
293
294\begin{verbatim}
Fred Drakec55ae4b2002-04-19 04:04:57 +0000295cl /LD /I/python/include spam.c ../libs/pythonXY.lib
296cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib
Fred Drakecc8f44b2001-08-20 19:30:29 +0000297\end{verbatim}
298
299The first command created three files: \file{spam.obj},
300\file{spam.dll} and \file{spam.lib}. \file{Spam.dll} does not contain
301any Python functions (such as \cfunction{PyArg_ParseTuple()}), but it
Fred Drakec55ae4b2002-04-19 04:04:57 +0000302does know how to find the Python code thanks to \file{pythonXY.lib}.
Fred Drakecc8f44b2001-08-20 19:30:29 +0000303
304The second command created \file{ni.dll} (and \file{.obj} and
305\file{.lib}), which knows how to find the necessary functions from
306spam, and also from the Python executable.
307
308Not every identifier is exported to the lookup table. If you want any
309other modules (including Python) to be able to see your identifiers,
310you have to say \samp{_declspec(dllexport)}, as in \samp{void
311_declspec(dllexport) initspam(void)} or \samp{PyObject
312_declspec(dllexport) *NiGetSpamData(void)}.
313
314Developer Studio will throw in a lot of import libraries that you do
315not really need, adding about 100K to your executable. To get rid of
316them, use the Project Settings dialog, Link tab, to specify
317\emph{ignore default libraries}. Add the correct
318\file{msvcrt\var{xx}.lib} to the list of libraries.