blob: e8ee4e1558bd8401a3afe050d476976ab0094cfc [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,
George Yoshidab2d5af82006-07-15 16:03:49 +000031just as there are on \UNIX: use the
32\citetitle[../lib/module-distutils.html]{distutils} package to
Fred Drake5f293192001-12-13 17:20:32 +000033control the build process, or do things manually. The distutils
34approach works well for most extensions; documentation on using
George Yoshidab2d5af82006-07-15 16:03:49 +000035\citetitle[../lib/module-distutils.html]{distutils} to build and
36package extension modules is available in
37\citetitle[../dist/dist.html]{Distributing Python Modules}. This
38section describes the manual approach to building Python extensions
39written in C or \Cpp.
Fred Drakecc8f44b2001-08-20 19:30:29 +000040
Fred Drake5f293192001-12-13 17:20:32 +000041To build extensions using these instructions, you need to have a copy
42of the Python sources of the same version as your installed Python.
43You will need Microsoft Visual \Cpp{} ``Developer Studio''; project
Martin v. Löwis658b50f2004-12-30 10:44:32 +000044files are supplied for V\Cpp{} version 7.1, but you can use older
45versions of V\Cpp. Notice that you should use the same version of
46V\Cpp that was used to build Python itself. The example files
47described here are distributed with the Python sources in the
48\file{PC\textbackslash example_nt\textbackslash} directory.
Fred Drakecc8f44b2001-08-20 19:30:29 +000049
Fred Drake5f293192001-12-13 17:20:32 +000050\begin{enumerate}
51 \item
52 \strong{Copy the example files}\\
53 The \file{example_nt} directory is a subdirectory of the \file{PC}
54 directory, in order to keep all the PC-specific files under the
55 same directory in the source distribution. However, the
56 \file{example_nt} directory can't actually be used from this
57 location. You first need to copy or move it up one level, so that
58 \file{example_nt} is a sibling of the \file{PC} and \file{Include}
59 directories. Do all your work from within this new location.
Fred Drakecc8f44b2001-08-20 19:30:29 +000060
Fred Drake5f293192001-12-13 17:20:32 +000061 \item
62 \strong{Open the project}\\
Martin v. Löwis658b50f2004-12-30 10:44:32 +000063 From V\Cpp, use the \menuselection{File \sub Open Solution}
Fred Drake5f293192001-12-13 17:20:32 +000064 dialog (not \menuselection{File \sub Open}!). Navigate to and
Martin v. Löwis658b50f2004-12-30 10:44:32 +000065 select the file \file{example.sln}, in the \emph{copy} of the
Fred Drake5f293192001-12-13 17:20:32 +000066 \file{example_nt} directory you made above. Click Open.
Fred Drakecc8f44b2001-08-20 19:30:29 +000067
Fred Drake5f293192001-12-13 17:20:32 +000068 \item
69 \strong{Build the example DLL}\\
70 In order to check that everything is set up right, try building:
Fred Drakecc8f44b2001-08-20 19:30:29 +000071
Fred Drake5f293192001-12-13 17:20:32 +000072 \begin{enumerate}
73 \item
74 Select a configuration. This step is optional. Choose
Martin v. Löwis658b50f2004-12-30 10:44:32 +000075 \menuselection{Build \sub Configuration Manager \sub Active
76 Solution Configuration} and select either \guilabel{Release}
77 or\guilabel{Debug}. If you skip this step,
Fred Drake0d8da3a2004-01-23 09:01:56 +000078 V\Cpp{} will use the Debug configuration by default.
Fred Drakecc8f44b2001-08-20 19:30:29 +000079
Fred Drake5f293192001-12-13 17:20:32 +000080 \item
81 Build the DLL. Choose \menuselection{Build \sub Build
Martin v. Löwis658b50f2004-12-30 10:44:32 +000082 Solution}. This creates all intermediate and result files in
83 a subdirectory called either \file{Debug} or \file{Release},
84 depending on which configuration you selected in the preceding
85 step.
Fred Drake5f293192001-12-13 17:20:32 +000086 \end{enumerate}
87
88 \item
89 \strong{Testing the debug-mode DLL}\\
90 Once the Debug build has succeeded, bring up a DOS box, and change
91 to the \file{example_nt\textbackslash Debug} directory. You
92 should now be able to repeat the following session (\code{C>} is
Fred Drakef25fa6d2006-05-03 02:04:40 +000093 the DOS prompt, \code{>>>} is the Python prompt; note that
Fred Drake5f293192001-12-13 17:20:32 +000094 build information and various debug output from Python may not
95 match this screen dump exactly):
96
97\begin{verbatim}
98C>..\..\PCbuild\python_d
99Adding parser accelerators ...
100Done.
101Python 2.2 (#28, Dec 19 2001, 23:26:37) [MSC 32 bit (Intel)] on win32
102Type "copyright", "credits" or "license" for more information.
103>>> import example
104[4897 refs]
105>>> example.foo()
106Hello, world
107[4903 refs]
108>>>
109\end{verbatim}
110
111 Congratulations! You've successfully built your first Python
112 extension module.
113
114 \item
Thomas Heller291e9ee2002-07-04 08:36:53 +0000115 \strong{Creating your own project}\\
Fred Drake5f293192001-12-13 17:20:32 +0000116 Choose a name and create a directory for it. Copy your C sources
117 into it. Note that the module source file name does not
118 necessarily have to match the module name, but the name of the
119 initialization function should match the module name --- you can
120 only import a module \module{spam} if its initialization function
121 is called \cfunction{initspam()}, and it should call
122 \cfunction{Py_InitModule()} with the string \code{"spam"} as its
123 first argument (use the minimal \file{example.c} in this directory
124 as a guide). By convention, it lives in a file called
125 \file{spam.c} or \file{spammodule.c}. The output file should be
126 called \file{spam.dll} or \file{spam.pyd} (the latter is supported
127 to avoid confusion with a system library \file{spam.dll} to which
128 your module could be a Python interface) in Release mode, or
129 \file{spam_d.dll} or \file{spam_d.pyd} in Debug mode.
130
131 Now your options are:
132
133 \begin{enumerate}
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000134 \item Copy \file{example.sln} and \file{example.vcproj}, rename
Fred Drake5f293192001-12-13 17:20:32 +0000135 them to \file{spam.*}, and edit them by hand, or
136 \item Create a brand new project; instructions are below.
137 \end{enumerate}
138
139 In either case, copy \file{example_nt\textbackslash example.def}
140 to \file{spam\textbackslash spam.def}, and edit the new
141 \file{spam.def} so its second line contains the string
142 `\code{initspam}'. If you created a new project yourself, add the
143 file \file{spam.def} to the project now. (This is an annoying
144 little file with only two lines. An alternative approach is to
145 forget about the \file{.def} file, and add the option
146 \programopt{/export:initspam} somewhere to the Link settings, by
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000147 manually editing the setting in Project Properties dialog).
Fred Drake5f293192001-12-13 17:20:32 +0000148
149 \item
150 \strong{Creating a brand new project}\\
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000151 Use the \menuselection{File \sub New \sub Project} dialog to
152 create a new Project Workspace. Select \guilabel{Visual C++
153 Projects/Win32/ Win32 Project}, enter the name (\samp{spam}), and
154 make sure the Location is set to parent of the \file{spam}
155 directory you have created (which should be a direct subdirectory
156 of the Python build tree, a sibling of \file{Include} and
157 \file{PC}). Select Win32 as the platform (in my version, this is
158 the only choice). Make sure the Create new workspace radio button
159 is selected. Click OK.
Fred Drake5f293192001-12-13 17:20:32 +0000160
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000161 You should now create the file \file{spam.def} as instructed in
162 the previous section. Add the source files to the project, using
163 \menuselection{Project \sub Add Existing Item}. Set the pattern to
164 \code{*.*} and select both \file{spam.c} and \file{spam.def} and
165 click OK. (Inserting them one by one is fine too.)
166
167 Now open the \menuselection{Project \sub spam properties} dialog.
Martin v. Löwisd61788b2005-01-03 23:42:01 +0000168 You only need to change a few settings. Make sure \guilabel{All
169 Configurations} is selected from the \guilabel{Settings for:}
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000170 dropdown list. Select the C/\Cpp{} tab. Choose the General
171 category in the popup menu at the top. Type the following text in
172 the entry box labeled \guilabel{Additional Include Directories}:
Fred Drake5f293192001-12-13 17:20:32 +0000173
174\begin{verbatim}
175..\Include,..\PC
176\end{verbatim}
177
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000178 Then, choose the General category in the Linker tab, and enter
Fred Drake5f293192001-12-13 17:20:32 +0000179
180\begin{verbatim}
181..\PCbuild
182\end{verbatim}
183
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000184 in the text box labelled \guilabel{Additional library Directories}.
Fred Drake5f293192001-12-13 17:20:32 +0000185
186 Now you need to add some mode-specific settings:
187
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000188 Select \guilabel{Release} in the \guilabel{Configuration}
Fred Drake0d8da3a2004-01-23 09:01:56 +0000189 dropdown list. Choose the \guilabel{Link} tab, choose the
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000190 \guilabel{Input} category, and append \code{pythonXY.lib} to the
191 list in the \guilabel{Additional Dependencies} box.
Fred Drake5f293192001-12-13 17:20:32 +0000192
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000193 Select \guilabel{Debug} in the \guilabel{Configuration} dropdown
194 list, and append \code{pythonXY_d.lib} to the list in the
195 \guilabel{Additional Dependencies} box. Then click the C/\Cpp{}
196 tab, select \guilabel{Code Generation}, and select
197 \guilabel{Multi-threaded Debug DLL} from the \guilabel{Runtime
198 library} dropdown list.
Fred Drake5f293192001-12-13 17:20:32 +0000199
Martin v. Löwis658b50f2004-12-30 10:44:32 +0000200 Select \guilabel{Release} again from the \guilabel{Configuration}
201 dropdown list. Select \guilabel{Multi-threaded DLL} from the
202 \guilabel{Runtime library} dropdown list.
Fred Drake5f293192001-12-13 17:20:32 +0000203\end{enumerate}
204
Fred Drakecc8f44b2001-08-20 19:30:29 +0000205
206If your module creates a new type, you may have trouble with this line:
207
208\begin{verbatim}
209 PyObject_HEAD_INIT(&PyType_Type)
210\end{verbatim}
211
212Change it to:
213
214\begin{verbatim}
215 PyObject_HEAD_INIT(NULL)
216\end{verbatim}
217
218and add the following to the module initialization function:
219
220\begin{verbatim}
221 MyObject_Type.ob_type = &PyType_Type;
222\end{verbatim}
223
Fred Drakecc6cc5d2002-11-05 16:52:50 +0000224Refer to section~3 of the
Fred Drakecc8f44b2001-08-20 19:30:29 +0000225\citetitle[http://www.python.org/doc/FAQ.html]{Python FAQ} for details
226on why you must do this.
227
228
229\section{Differences Between \UNIX{} and Windows
230 \label{dynamic-linking}}
231\sectionauthor{Chris Phoenix}{cphoenix@best.com}
232
233
234\UNIX{} and Windows use completely different paradigms for run-time
235loading of code. Before you try to build a module that can be
236dynamically loaded, be aware of how your system works.
237
Fred Drakec37b65e2001-11-28 07:26:15 +0000238In \UNIX, a shared object (\file{.so}) file contains code to be used by the
Fred Drakecc8f44b2001-08-20 19:30:29 +0000239program, and also the names of functions and data that it expects to
240find in the program. When the file is joined to the program, all
241references to those functions and data in the file's code are changed
242to point to the actual locations in the program where the functions
243and data are placed in memory. This is basically a link operation.
244
245In Windows, a dynamic-link library (\file{.dll}) file has no dangling
246references. Instead, an access to functions or data goes through a
247lookup table. So the DLL code does not have to be fixed up at runtime
248to refer to the program's memory; instead, the code already uses the
249DLL's lookup table, and the lookup table is modified at runtime to
250point to the functions and data.
251
Fred Drakec37b65e2001-11-28 07:26:15 +0000252In \UNIX, there is only one type of library file (\file{.a}) which
Fred Drakecc8f44b2001-08-20 19:30:29 +0000253contains code from several object files (\file{.o}). During the link
254step to create a shared object file (\file{.so}), the linker may find
255that it doesn't know where an identifier is defined. The linker will
256look for it in the object files in the libraries; if it finds it, it
257will include all the code from that object file.
258
259In Windows, there are two types of library, a static library and an
260import library (both called \file{.lib}). A static library is like a
261\UNIX{} \file{.a} file; it contains code to be included as necessary.
262An import library is basically used only to reassure the linker that a
263certain identifier is legal, and will be present in the program when
264the DLL is loaded. So the linker uses the information from the
265import library to build the lookup table for using identifiers that
266are not included in the DLL. When an application or a DLL is linked,
267an import library may be generated, which will need to be used for all
268future DLLs that depend on the symbols in the application or DLL.
269
270Suppose you are building two dynamic-load modules, B and C, which should
Fred Drakec37b65e2001-11-28 07:26:15 +0000271share another block of code A. On \UNIX, you would \emph{not} pass
Fred Drakecc8f44b2001-08-20 19:30:29 +0000272\file{A.a} to the linker for \file{B.so} and \file{C.so}; that would
273cause it to be included twice, so that B and C would each have their
274own copy. In Windows, building \file{A.dll} will also build
275\file{A.lib}. You \emph{do} pass \file{A.lib} to the linker for B and
276C. \file{A.lib} does not contain code; it just contains information
277which will be used at runtime to access A's code.
278
279In Windows, using an import library is sort of like using \samp{import
280spam}; it gives you access to spam's names, but does not create a
Fred Drakec37b65e2001-11-28 07:26:15 +0000281separate copy. On \UNIX, linking with a library is more like
Fred Drakecc8f44b2001-08-20 19:30:29 +0000282\samp{from spam import *}; it does create a separate copy.
283
284
285\section{Using DLLs in Practice \label{win-dlls}}
286\sectionauthor{Chris Phoenix}{cphoenix@best.com}
287
Fred Drakec37b65e2001-11-28 07:26:15 +0000288Windows Python is built in Microsoft Visual \Cpp; using other
Fred Drakecc8f44b2001-08-20 19:30:29 +0000289compilers may or may not work (though Borland seems to). The rest of
290this section is MSV\Cpp{} specific.
291
Fred Drakec55ae4b2002-04-19 04:04:57 +0000292When creating DLLs in Windows, you must pass \file{pythonXY.lib} to
Fred Drakecc8f44b2001-08-20 19:30:29 +0000293the linker. To build two DLLs, spam and ni (which uses C functions
294found in spam), you could use these commands:
295
296\begin{verbatim}
Fred Drakec55ae4b2002-04-19 04:04:57 +0000297cl /LD /I/python/include spam.c ../libs/pythonXY.lib
298cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib
Fred Drakecc8f44b2001-08-20 19:30:29 +0000299\end{verbatim}
300
301The first command created three files: \file{spam.obj},
302\file{spam.dll} and \file{spam.lib}. \file{Spam.dll} does not contain
303any Python functions (such as \cfunction{PyArg_ParseTuple()}), but it
Fred Drakec55ae4b2002-04-19 04:04:57 +0000304does know how to find the Python code thanks to \file{pythonXY.lib}.
Fred Drakecc8f44b2001-08-20 19:30:29 +0000305
306The second command created \file{ni.dll} (and \file{.obj} and
307\file{.lib}), which knows how to find the necessary functions from
308spam, and also from the Python executable.
309
310Not every identifier is exported to the lookup table. If you want any
311other modules (including Python) to be able to see your identifiers,
312you have to say \samp{_declspec(dllexport)}, as in \samp{void
313_declspec(dllexport) initspam(void)} or \samp{PyObject
314_declspec(dllexport) *NiGetSpamData(void)}.
315
316Developer Studio will throw in a lot of import libraries that you do
317not really need, adding about 100K to your executable. To get rid of
318them, use the Project Settings dialog, Link tab, to specify
319\emph{ignore default libraries}. Add the correct
320\file{msvcrt\var{xx}.lib} to the list of libraries.