1 .. highlightlang:: c 2 3 4 .. _building-on-windows: 5 6 **************************************** 7 Building C and C++ Extensions on Windows 8 **************************************** 9 10 This chapter briefly explains how to create a Windows extension module for 11 Python using Microsoft Visual C++, and follows with more detailed background 12 information on how it works. The explanatory material is useful for both the 13 Windows programmer learning to build Python extensions and the Unix programmer 14 interested in producing software which can be successfully built on both Unix 15 and Windows. 16 17 Module authors are encouraged to use the distutils approach for building 18 extension modules, instead of the one described in this section. You will still 19 need the C compiler that was used to build Python; typically Microsoft Visual 20 C++. 21 22 .. note:: 23 24 This chapter mentions a number of filenames that include an encoded Python 25 version number. These filenames are represented with the version number shown 26 as ``XY``; in practice, ``'X'`` will be the major version number and ``'Y'`` 27 will be the minor version number of the Python release you're working with. For 28 example, if you are using Python 2.2.1, ``XY`` will actually be ``22``. 29 30 31 .. _win-cookbook: 32 33 A Cookbook Approach 34 =================== 35 36 There are two approaches to building extension modules on Windows, just as there 37 are on Unix: use the :mod:`distutils` package to control the build process, or 38 do things manually. The distutils approach works well for most extensions; 39 documentation on using :mod:`distutils` to build and package extension modules 40 is available in :ref:`distutils-index`. If you find you really need to do 41 things manually, it may be instructive to study the project file for the 42 :source:`winsound <PCbuild/winsound.vcxproj>` standard library module. 43 44 45 .. _dynamic-linking: 46 47 Differences Between Unix and Windows 48 ==================================== 49 50 .. sectionauthor:: Chris Phoenix <cphoenix (a] best.com> 51 52 53 Unix and Windows use completely different paradigms for run-time loading of 54 code. Before you try to build a module that can be dynamically loaded, be aware 55 of how your system works. 56 57 In Unix, a shared object (:file:`.so`) file contains code to be used by the 58 program, and also the names of functions and data that it expects to find in the 59 program. When the file is joined to the program, all references to those 60 functions and data in the file's code are changed to point to the actual 61 locations in the program where the functions and data are placed in memory. 62 This is basically a link operation. 63 64 In Windows, a dynamic-link library (:file:`.dll`) file has no dangling 65 references. Instead, an access to functions or data goes through a lookup 66 table. So the DLL code does not have to be fixed up at runtime to refer to the 67 program's memory; instead, the code already uses the DLL's lookup table, and the 68 lookup table is modified at runtime to point to the functions and data. 69 70 In Unix, there is only one type of library file (:file:`.a`) which contains code 71 from several object files (:file:`.o`). During the link step to create a shared 72 object file (:file:`.so`), the linker may find that it doesn't know where an 73 identifier is defined. The linker will look for it in the object files in the 74 libraries; if it finds it, it will include all the code from that object file. 75 76 In Windows, there are two types of library, a static library and an import 77 library (both called :file:`.lib`). A static library is like a Unix :file:`.a` 78 file; it contains code to be included as necessary. An import library is 79 basically used only to reassure the linker that a certain identifier is legal, 80 and will be present in the program when the DLL is loaded. So the linker uses 81 the information from the import library to build the lookup table for using 82 identifiers that are not included in the DLL. When an application or a DLL is 83 linked, an import library may be generated, which will need to be used for all 84 future DLLs that depend on the symbols in the application or DLL. 85 86 Suppose you are building two dynamic-load modules, B and C, which should share 87 another block of code A. On Unix, you would *not* pass :file:`A.a` to the 88 linker for :file:`B.so` and :file:`C.so`; that would cause it to be included 89 twice, so that B and C would each have their own copy. In Windows, building 90 :file:`A.dll` will also build :file:`A.lib`. You *do* pass :file:`A.lib` to the 91 linker for B and C. :file:`A.lib` does not contain code; it just contains 92 information which will be used at runtime to access A's code. 93 94 In Windows, using an import library is sort of like using ``import spam``; it 95 gives you access to spam's names, but does not create a separate copy. On Unix, 96 linking with a library is more like ``from spam import *``; it does create a 97 separate copy. 98 99 100 .. _win-dlls: 101 102 Using DLLs in Practice 103 ====================== 104 105 .. sectionauthor:: Chris Phoenix <cphoenix (a] best.com> 106 107 108 Windows Python is built in Microsoft Visual C++; using other compilers may or 109 may not work (though Borland seems to). The rest of this section is MSVC++ 110 specific. 111 112 When creating DLLs in Windows, you must pass :file:`pythonXY.lib` to the linker. 113 To build two DLLs, spam and ni (which uses C functions found in spam), you could 114 use these commands:: 115 116 cl /LD /I/python/include spam.c ../libs/pythonXY.lib 117 cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib 118 119 The first command created three files: :file:`spam.obj`, :file:`spam.dll` and 120 :file:`spam.lib`. :file:`Spam.dll` does not contain any Python functions (such 121 as :c:func:`PyArg_ParseTuple`), but it does know how to find the Python code 122 thanks to :file:`pythonXY.lib`. 123 124 The second command created :file:`ni.dll` (and :file:`.obj` and :file:`.lib`), 125 which knows how to find the necessary functions from spam, and also from the 126 Python executable. 127 128 Not every identifier is exported to the lookup table. If you want any other 129 modules (including Python) to be able to see your identifiers, you have to say 130 ``_declspec(dllexport)``, as in ``void _declspec(dllexport) initspam(void)`` or 131 ``PyObject _declspec(dllexport) *NiGetSpamData(void)``. 132 133 Developer Studio will throw in a lot of import libraries that you do not really 134 need, adding about 100K to your executable. To get rid of them, use the Project 135 Settings dialog, Link tab, to specify *ignore default libraries*. Add the 136 correct :file:`msvcrtxx.lib` to the list of libraries. 137 138