Debian Codegraph @

Doc/extending/windows.rst

raw
1 .. highlight:: 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 ``setuptools`` package to control the build process, or
38 do things manually. The setuptools approach works well for most extensions;
39 documentation on using ``setuptools`` to build and package extension modules
40 is available in :ref:`setuptools-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 <[email protected]>
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 <[email protected]>
106
107
108 Windows Python is built in Microsoft Visual C++; using other compilers may or
109 may not work. The rest of this section is MSVC++ specific.
110
111 When creating DLLs in Windows, you must pass :file:`pythonXY.lib` to the linker.
112 To build two DLLs, spam and ni (which uses C functions found in spam), you could
113 use these commands::
114
115 cl /LD /I/python/include spam.c ../libs/pythonXY.lib
116 cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib
117
118 The first command created three files: :file:`spam.obj`, :file:`spam.dll` and
119 :file:`spam.lib`. :file:`Spam.dll` does not contain any Python functions (such
120 as :c:func:`PyArg_ParseTuple`), but it does know how to find the Python code
121 thanks to :file:`pythonXY.lib`.
122
123 The second command created :file:`ni.dll` (and :file:`.obj` and :file:`.lib`),
124 which knows how to find the necessary functions from spam, and also from the
125 Python executable.
126
127 Not every identifier is exported to the lookup table. If you want any other
128 modules (including Python) to be able to see your identifiers, you have to say
129 ``_declspec(dllexport)``, as in ``void _declspec(dllexport) initspam(void)`` or
130 ``PyObject _declspec(dllexport) *NiGetSpamData(void)``.
131
132 Developer Studio will throw in a lot of import libraries that you do not really
133 need, adding about 100K to your executable. To get rid of them, use the Project
134 Settings dialog, Link tab, to specify *ignore default libraries*. Add the
135 correct :file:`msvcrt{xx}.lib` to the list of libraries.