Fred Drake | 9651198 | 2002-04-05 19:54:19 +0000 | [diff] [blame] | 1 | \chapter{Building C and \Cpp{} Extensions on Windows% |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 2 | \label{building-on-windows}} |
| 3 | |
| 4 | |
| 5 | This chapter briefly explains how to create a Windows extension module |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 6 | for Python using Microsoft Visual \Cpp, and follows with more |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 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. |
| 11 | |
Martin v. Löwis | 27761f3 | 2002-03-09 10:06:14 +0000 | [diff] [blame] | 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. |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 16 | |
Fred Drake | c55ae4b | 2002-04-19 04:04:57 +0000 | [diff] [blame] | 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} |
| 26 | |
| 27 | |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 28 | \section{A Cookbook Approach \label{win-cookbook}} |
| 29 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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. |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 38 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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. |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 46 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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. |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 57 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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. |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 64 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 65 | \item |
| 66 | \strong{Build the example DLL}\\ |
| 67 | In order to check that everything is set up right, try building: |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 68 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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. |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 76 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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} |
| 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} |
| 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] |
| 106 | >>> |
| 107 | \end{verbatim} |
| 108 | |
| 109 | Congratulations! You've successfully built your first Python |
| 110 | extension module. |
| 111 | |
| 112 | \item |
Thomas Heller | 291e9ee | 2002-07-04 08:36:53 +0000 | [diff] [blame] | 113 | \strong{Creating your own project}\\ |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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. |
| 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 Drake | c55ae4b | 2002-04-19 04:04:57 +0000 | [diff] [blame] | 181 | \code{pythonXY.lib} to the list in the ``Object/library modules'' |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 182 | box. |
| 183 | |
| 184 | Select ``Win32 Debug'' in the ``Settings for'' dropdown list, and |
Fred Drake | c55ae4b | 2002-04-19 04:04:57 +0000 | [diff] [blame] | 185 | append \code{pythonXY_d.lib} to the list in the ``Object/library |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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. |
| 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 Drake | cc6cc5d | 2002-11-05 16:52:50 +0000 | [diff] [blame] | 195 | You should now create the file \file{spam.def} as instructed in the |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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} |
| 201 | |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 202 | |
| 203 | If 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 | |
| 209 | Change it to: |
| 210 | |
| 211 | \begin{verbatim} |
| 212 | PyObject_HEAD_INIT(NULL) |
| 213 | \end{verbatim} |
| 214 | |
| 215 | and add the following to the module initialization function: |
| 216 | |
| 217 | \begin{verbatim} |
| 218 | MyObject_Type.ob_type = &PyType_Type; |
| 219 | \end{verbatim} |
| 220 | |
Fred Drake | cc6cc5d | 2002-11-05 16:52:50 +0000 | [diff] [blame] | 221 | Refer to section~3 of the |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 222 | \citetitle[http://www.python.org/doc/FAQ.html]{Python FAQ} for details |
| 223 | on 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 |
| 232 | loading of code. Before you try to build a module that can be |
| 233 | dynamically loaded, be aware of how your system works. |
| 234 | |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 235 | In \UNIX, a shared object (\file{.so}) file contains code to be used by the |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 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. |
| 241 | |
| 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. |
| 248 | |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 249 | In \UNIX, there is only one type of library file (\file{.a}) which |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 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. |
| 255 | |
| 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. |
| 266 | |
| 267 | Suppose you are building two dynamic-load modules, B and C, which should |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 268 | share another block of code A. On \UNIX, you would \emph{not} pass |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 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. |
| 275 | |
| 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 |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 278 | separate copy. On \UNIX, linking with a library is more like |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 279 | \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 Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 285 | Windows Python is built in Microsoft Visual \Cpp; using other |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 286 | compilers may or may not work (though Borland seems to). The rest of |
| 287 | this section is MSV\Cpp{} specific. |
| 288 | |
Fred Drake | c55ae4b | 2002-04-19 04:04:57 +0000 | [diff] [blame] | 289 | When creating DLLs in Windows, you must pass \file{pythonXY.lib} to |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 290 | the linker. To build two DLLs, spam and ni (which uses C functions |
| 291 | found in spam), you could use these commands: |
| 292 | |
| 293 | \begin{verbatim} |
Fred Drake | c55ae4b | 2002-04-19 04:04:57 +0000 | [diff] [blame] | 294 | cl /LD /I/python/include spam.c ../libs/pythonXY.lib |
| 295 | cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 296 | \end{verbatim} |
| 297 | |
| 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 |
Fred Drake | c55ae4b | 2002-04-19 04:04:57 +0000 | [diff] [blame] | 301 | does know how to find the Python code thanks to \file{pythonXY.lib}. |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 302 | |
| 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. |
| 306 | |
| 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)}. |
| 312 | |
| 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. |