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