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 |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 42 | files are supplied for V\Cpp{} version 7.1, but you can use older |
| 43 | versions of V\Cpp. Notice that you should use the same version of |
| 44 | V\Cpp that was used to build Python itself. The example files |
| 45 | described here are distributed with the Python sources in the |
| 46 | \file{PC\textbackslash example_nt\textbackslash} directory. |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 47 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 48 | \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 Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 58 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 59 | \item |
| 60 | \strong{Open the project}\\ |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 61 | From V\Cpp, use the \menuselection{File \sub Open Solution} |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 62 | dialog (not \menuselection{File \sub Open}!). Navigate to and |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 63 | select the file \file{example.sln}, in the \emph{copy} of the |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 64 | \file{example_nt} directory you made above. Click Open. |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 65 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 66 | \item |
| 67 | \strong{Build the example DLL}\\ |
| 68 | In order to check that everything is set up right, try building: |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 69 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 70 | \begin{enumerate} |
| 71 | \item |
| 72 | Select a configuration. This step is optional. Choose |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 73 | \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 Drake | 0d8da3a | 2004-01-23 09:01:56 +0000 | [diff] [blame] | 76 | V\Cpp{} will use the Debug configuration by default. |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 77 | |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 78 | \item |
| 79 | Build the DLL. Choose \menuselection{Build \sub Build |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 80 | 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 Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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} |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 132 | \item Copy \file{example.sln} and \file{example.vcproj}, rename |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 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 |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 145 | manually editing the setting in Project Properties dialog). |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 146 | |
| 147 | \item |
| 148 | \strong{Creating a brand new project}\\ |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 149 | 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 Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 158 | |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 159 | 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öwis | d61788b | 2005-01-03 23:42:01 +0000 | [diff] [blame] | 166 | You only need to change a few settings. Make sure \guilabel{All |
| 167 | Configurations} is selected from the \guilabel{Settings for:} |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 168 | 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 Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 171 | |
| 172 | \begin{verbatim} |
| 173 | ..\Include,..\PC |
| 174 | \end{verbatim} |
| 175 | |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 176 | Then, choose the General category in the Linker tab, and enter |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 177 | |
| 178 | \begin{verbatim} |
| 179 | ..\PCbuild |
| 180 | \end{verbatim} |
| 181 | |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 182 | in the text box labelled \guilabel{Additional library Directories}. |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 183 | |
| 184 | Now you need to add some mode-specific settings: |
| 185 | |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 186 | Select \guilabel{Release} in the \guilabel{Configuration} |
Fred Drake | 0d8da3a | 2004-01-23 09:01:56 +0000 | [diff] [blame] | 187 | dropdown list. Choose the \guilabel{Link} tab, choose the |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 188 | \guilabel{Input} category, and append \code{pythonXY.lib} to the |
| 189 | list in the \guilabel{Additional Dependencies} box. |
Fred Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 190 | |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 191 | 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 Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 197 | |
Martin v. Löwis | 658b50f | 2004-12-30 10:44:32 +0000 | [diff] [blame] | 198 | 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 Drake | 5f29319 | 2001-12-13 17:20:32 +0000 | [diff] [blame] | 201 | \end{enumerate} |
| 202 | |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 203 | |
| 204 | If 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 | |
| 210 | Change it to: |
| 211 | |
| 212 | \begin{verbatim} |
| 213 | PyObject_HEAD_INIT(NULL) |
| 214 | \end{verbatim} |
| 215 | |
| 216 | and add the following to the module initialization function: |
| 217 | |
| 218 | \begin{verbatim} |
| 219 | MyObject_Type.ob_type = &PyType_Type; |
| 220 | \end{verbatim} |
| 221 | |
Fred Drake | cc6cc5d | 2002-11-05 16:52:50 +0000 | [diff] [blame] | 222 | Refer to section~3 of the |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 223 | \citetitle[http://www.python.org/doc/FAQ.html]{Python FAQ} for details |
| 224 | on 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 |
| 233 | loading of code. Before you try to build a module that can be |
| 234 | dynamically loaded, be aware of how your system works. |
| 235 | |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 236 | 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] | 237 | program, and also the names of functions and data that it expects to |
| 238 | find in the program. When the file is joined to the program, all |
| 239 | references to those functions and data in the file's code are changed |
| 240 | to point to the actual locations in the program where the functions |
| 241 | and data are placed in memory. This is basically a link operation. |
| 242 | |
| 243 | In Windows, a dynamic-link library (\file{.dll}) file has no dangling |
| 244 | references. Instead, an access to functions or data goes through a |
| 245 | lookup table. So the DLL code does not have to be fixed up at runtime |
| 246 | to refer to the program's memory; instead, the code already uses the |
| 247 | DLL's lookup table, and the lookup table is modified at runtime to |
| 248 | point to the functions and data. |
| 249 | |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 250 | 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] | 251 | contains code from several object files (\file{.o}). During the link |
| 252 | step to create a shared object file (\file{.so}), the linker may find |
| 253 | that it doesn't know where an identifier is defined. The linker will |
| 254 | look for it in the object files in the libraries; if it finds it, it |
| 255 | will include all the code from that object file. |
| 256 | |
| 257 | In Windows, there are two types of library, a static library and an |
| 258 | import library (both called \file{.lib}). A static library is like a |
| 259 | \UNIX{} \file{.a} file; it contains code to be included as necessary. |
| 260 | An import library is basically used only to reassure the linker that a |
| 261 | certain identifier is legal, and will be present in the program when |
| 262 | the DLL is loaded. So the linker uses the information from the |
| 263 | import library to build the lookup table for using identifiers that |
| 264 | are not included in the DLL. When an application or a DLL is linked, |
| 265 | an import library may be generated, which will need to be used for all |
| 266 | future DLLs that depend on the symbols in the application or DLL. |
| 267 | |
| 268 | 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] | 269 | 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] | 270 | \file{A.a} to the linker for \file{B.so} and \file{C.so}; that would |
| 271 | cause it to be included twice, so that B and C would each have their |
| 272 | own 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 |
| 274 | C. \file{A.lib} does not contain code; it just contains information |
| 275 | which will be used at runtime to access A's code. |
| 276 | |
| 277 | In Windows, using an import library is sort of like using \samp{import |
| 278 | 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] | 279 | separate copy. On \UNIX, linking with a library is more like |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 280 | \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 Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 286 | Windows Python is built in Microsoft Visual \Cpp; using other |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 287 | compilers may or may not work (though Borland seems to). The rest of |
| 288 | this section is MSV\Cpp{} specific. |
| 289 | |
Fred Drake | c55ae4b | 2002-04-19 04:04:57 +0000 | [diff] [blame] | 290 | When creating DLLs in Windows, you must pass \file{pythonXY.lib} to |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 291 | the linker. To build two DLLs, spam and ni (which uses C functions |
| 292 | found in spam), you could use these commands: |
| 293 | |
| 294 | \begin{verbatim} |
Fred Drake | c55ae4b | 2002-04-19 04:04:57 +0000 | [diff] [blame] | 295 | cl /LD /I/python/include spam.c ../libs/pythonXY.lib |
| 296 | cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib |
Fred Drake | cc8f44b | 2001-08-20 19:30:29 +0000 | [diff] [blame] | 297 | \end{verbatim} |
| 298 | |
| 299 | The first command created three files: \file{spam.obj}, |
| 300 | \file{spam.dll} and \file{spam.lib}. \file{Spam.dll} does not contain |
| 301 | any Python functions (such as \cfunction{PyArg_ParseTuple()}), but it |
Fred Drake | c55ae4b | 2002-04-19 04:04:57 +0000 | [diff] [blame] | 302 | 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] | 303 | |
| 304 | The second command created \file{ni.dll} (and \file{.obj} and |
| 305 | \file{.lib}), which knows how to find the necessary functions from |
| 306 | spam, and also from the Python executable. |
| 307 | |
| 308 | Not every identifier is exported to the lookup table. If you want any |
| 309 | other modules (including Python) to be able to see your identifiers, |
| 310 | you have to say \samp{_declspec(dllexport)}, as in \samp{void |
| 311 | _declspec(dllexport) initspam(void)} or \samp{PyObject |
| 312 | _declspec(dllexport) *NiGetSpamData(void)}. |
| 313 | |
| 314 | Developer Studio will throw in a lot of import libraries that you do |
| 315 | not really need, adding about 100K to your executable. To get rid of |
| 316 | them, 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. |