Extending and Embedding Python, Release 3.9.5 2.5 Building C and C++ Extensions on Windows 2.5.1 A Cookbook Approach 2.5.2 Differences Between Unix and Windows 57 2.5. Building C and C++ Extensions on Windows This chapter briefly explains how to create a Windows extension module for Python using Microsoft Visual C++, and follows with more detailed background information on how it works. The explanatory material is useful for both the Windows programmer learning to build Python extensions and the Unix programmer interested in producing software which can be successfully built on both Unix and Windows. Module authors are encouraged to use the distutils approach for building extension modules, instead of the one described in this section. You will still need the C compiler that was used to build Python; typically Microsoft Visual C++. Note: This chapter mentions a number of filenames that include an encoded Python version number. These filenames are represented with the version number shown as XY ; in practice, 'X' will be the major version number and 'Y' will be the minor version number of the Python release you’re working with. For example, if you are using Python 2.2.1,$_{XY}$ will actually be$_{22}$ . There are two approaches to building extension modules on Windows, just as there are on Unix: use the distutils package to control the build process, or do things manually. The distutils approach works well for most extensions; documentation on using$_{distutils}$to build and package extension modules is available in distutils-index. If you find you really need to do things manually, it may be instructive to study the project file for the winsound standard library module. Unix and Windows use completely different paradigms for run-time loading of code. Before you try to build a module that can be dynamically loaded, be aware of how your system works. In Unix, a shared object ($_{.so}$ ) file contains code to be used by the program, and also the names of functions and data that it expects to find in the program. When the file is joined to the program, all references to those functions and data in the file’s code are changed to point to the actual locations in the program where the functions and data are placed in memory. This is basically a link operation. In Windows, a dynamic-link library ($_{.dll}$ ) file has no dangling references. Instead, an access to functions or data goes through a lookup table. So the DLL code does not have to be fixed up at runtime to refer to the program’s memory; instead, the code already uses the DLL’s lookup table, and the lookup table is modified at runtime to point to the functions and data. In Unix, there is only one type of library file ($_{.a}$ ) which contains code from several object files ($_{.o}$ ). During the link step to create a shared object file ($_{.so}$ ), the linker may find that it doesn’t know where an identifier is defined. The linker will look for it in the object files in the libraries; if it finds it, it will include all the code from that object file. In Windows, there are two types of library, a static library and an import library (both called$_{.lib}$ ). A static library is like a Unix$_{.a}$file; it contains code to be included as necessary. An import library is basically used only to reassure the linker that a certain identifier is legal, and will be present in the program when the DLL is loaded. So the linker uses the information from the import library to build the lookup table for using identifiers that are not included in the DLL. When an application or a DLL is linked, an import library may be generated, which will need to be used for all future DLLs that depend on the symbols in the application or DLL. Suppose you are building two dynamic-load modules, B and C, which should share another block of code A. On Unix, you would not pass$_{A.a}$to the linker for$_{B.so}$and$_{C.so}$; that would cause it to be included twice, so that B and C would each have their own copy. In Windows, building$_{A.dll}$ will also build$_{A.lib}$ . You do pass$_{A.lib}$ to the linker for B and C.$_{A.lib}$does not contain code; it just contains information which will be used at runtime to access A’s code.