blob: d42263b3a215776b8674ccdb5683aebe4d053e4a [file] [log] [blame]
Fred Drakecc8f44b2001-08-20 19:30:29 +00001\chapter{Building C and \Cpp{} Extensions on Windows
2 \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
12
13\section{A Cookbook Approach \label{win-cookbook}}
14
15\sectionauthor{Neil Schemenauer}{neil_schemenauer@transcanada.com}
16
17This section provides a recipe for building a Python extension on
18Windows.
19
20Grab the binary installer from \url{http://www.python.org/} and
21install Python. The binary installer has all of the required header
22files except for \file{pyconfig.h}.
23
24Get the source distribution and extract it into a convenient location.
25Copy the \file{pyconfig.h} from the \file{PC/} directory into the
26\file{include/} directory created by the installer.
27
28Create a \file{Setup} file for your extension module, as described in
29chapter \ref{building-on-unix}.
30
31Get David Ascher's \file{compile.py} script from
32\url{http://starship.python.net/crew/da/compile/}. Run the script to
33create Microsoft Visual \Cpp{} project files.
34
35Open the DSW file in Visual \Cpp{} and select \strong{Build}.
36
37If your module creates a new type, you may have trouble with this line:
38
39\begin{verbatim}
40 PyObject_HEAD_INIT(&PyType_Type)
41\end{verbatim}
42
43Change it to:
44
45\begin{verbatim}
46 PyObject_HEAD_INIT(NULL)
47\end{verbatim}
48
49and add the following to the module initialization function:
50
51\begin{verbatim}
52 MyObject_Type.ob_type = &PyType_Type;
53\end{verbatim}
54
55Refer to section 3 of the
56\citetitle[http://www.python.org/doc/FAQ.html]{Python FAQ} for details
57on why you must do this.
58
59
60\section{Differences Between \UNIX{} and Windows
61 \label{dynamic-linking}}
62\sectionauthor{Chris Phoenix}{cphoenix@best.com}
63
64
65\UNIX{} and Windows use completely different paradigms for run-time
66loading of code. Before you try to build a module that can be
67dynamically loaded, be aware of how your system works.
68
Fred Drakec37b65e2001-11-28 07:26:15 +000069In \UNIX, a shared object (\file{.so}) file contains code to be used by the
Fred Drakecc8f44b2001-08-20 19:30:29 +000070program, and also the names of functions and data that it expects to
71find in the program. When the file is joined to the program, all
72references to those functions and data in the file's code are changed
73to point to the actual locations in the program where the functions
74and data are placed in memory. This is basically a link operation.
75
76In Windows, a dynamic-link library (\file{.dll}) file has no dangling
77references. Instead, an access to functions or data goes through a
78lookup table. So the DLL code does not have to be fixed up at runtime
79to refer to the program's memory; instead, the code already uses the
80DLL's lookup table, and the lookup table is modified at runtime to
81point to the functions and data.
82
Fred Drakec37b65e2001-11-28 07:26:15 +000083In \UNIX, there is only one type of library file (\file{.a}) which
Fred Drakecc8f44b2001-08-20 19:30:29 +000084contains code from several object files (\file{.o}). During the link
85step to create a shared object file (\file{.so}), the linker may find
86that it doesn't know where an identifier is defined. The linker will
87look for it in the object files in the libraries; if it finds it, it
88will include all the code from that object file.
89
90In Windows, there are two types of library, a static library and an
91import library (both called \file{.lib}). A static library is like a
92\UNIX{} \file{.a} file; it contains code to be included as necessary.
93An import library is basically used only to reassure the linker that a
94certain identifier is legal, and will be present in the program when
95the DLL is loaded. So the linker uses the information from the
96import library to build the lookup table for using identifiers that
97are not included in the DLL. When an application or a DLL is linked,
98an import library may be generated, which will need to be used for all
99future DLLs that depend on the symbols in the application or DLL.
100
101Suppose you are building two dynamic-load modules, B and C, which should
Fred Drakec37b65e2001-11-28 07:26:15 +0000102share another block of code A. On \UNIX, you would \emph{not} pass
Fred Drakecc8f44b2001-08-20 19:30:29 +0000103\file{A.a} to the linker for \file{B.so} and \file{C.so}; that would
104cause it to be included twice, so that B and C would each have their
105own copy. In Windows, building \file{A.dll} will also build
106\file{A.lib}. You \emph{do} pass \file{A.lib} to the linker for B and
107C. \file{A.lib} does not contain code; it just contains information
108which will be used at runtime to access A's code.
109
110In Windows, using an import library is sort of like using \samp{import
111spam}; it gives you access to spam's names, but does not create a
Fred Drakec37b65e2001-11-28 07:26:15 +0000112separate copy. On \UNIX, linking with a library is more like
Fred Drakecc8f44b2001-08-20 19:30:29 +0000113\samp{from spam import *}; it does create a separate copy.
114
115
116\section{Using DLLs in Practice \label{win-dlls}}
117\sectionauthor{Chris Phoenix}{cphoenix@best.com}
118
Fred Drakec37b65e2001-11-28 07:26:15 +0000119Windows Python is built in Microsoft Visual \Cpp; using other
Fred Drakecc8f44b2001-08-20 19:30:29 +0000120compilers may or may not work (though Borland seems to). The rest of
121this section is MSV\Cpp{} specific.
122
123When creating DLLs in Windows, you must pass \file{python15.lib} to
124the linker. To build two DLLs, spam and ni (which uses C functions
125found in spam), you could use these commands:
126
127\begin{verbatim}
128cl /LD /I/python/include spam.c ../libs/python15.lib
129cl /LD /I/python/include ni.c spam.lib ../libs/python15.lib
130\end{verbatim}
131
132The first command created three files: \file{spam.obj},
133\file{spam.dll} and \file{spam.lib}. \file{Spam.dll} does not contain
134any Python functions (such as \cfunction{PyArg_ParseTuple()}), but it
135does know how to find the Python code thanks to \file{python15.lib}.
136
137The second command created \file{ni.dll} (and \file{.obj} and
138\file{.lib}), which knows how to find the necessary functions from
139spam, and also from the Python executable.
140
141Not every identifier is exported to the lookup table. If you want any
142other modules (including Python) to be able to see your identifiers,
143you have to say \samp{_declspec(dllexport)}, as in \samp{void
144_declspec(dllexport) initspam(void)} or \samp{PyObject
145_declspec(dllexport) *NiGetSpamData(void)}.
146
147Developer Studio will throw in a lot of import libraries that you do
148not really need, adding about 100K to your executable. To get rid of
149them, use the Project Settings dialog, Link tab, to specify
150\emph{ignore default libraries}. Add the correct
151\file{msvcrt\var{xx}.lib} to the list of libraries.