blob: 142488256ec4e4911c7943371aa5f2c88be82cca [file] [log] [blame]
Guido van Rossum29168ce1997-05-16 16:17:20 +00001Example Python extension for Windows NT
2=======================================
3
4This directory contains everything you need to build a Python
5extension module using Microsoft VC++ 4.x ("Developer Studio"), except
6for the Python distribution. It has been tested most recently with
7version 4.2.
8
9The "example_nt" subdirectory should be an immediate subdirectory of
10the Python source directory -- a direct sibling of Include and PC, in
11particular, which are referenced as "..\Include" and "..\PC". In
12other words, it should *not* be used "as is". Copy or move it up one
13level or you will regret it! (This is done to keep all the PC
14specific files inside the PC subdirectory of the distribution, where
15they belong.)
16
17It is also assumed that the build results of Python are in the
18directory ..\vc40. In particular, the python15.lib file is referred
19to as "..\vc40\python15.lib".
20
21In order to use the example project from Developer Studio, use the
22"File->Open Workspace..." dialog (*not* the "File->Open..." dialog!).
23Change the pattern to "*.mak" and select the file "example.mak". Now
24choose "File->Save All" and the othe project files will be created.
25
26In order to check that everything is set up right, try building:
27choose "Build->Build example.dll". This creates all intermediate and
28result files in a subdirectory which is called either Debug or Release
29depending on which configuration you have chosen (as distributed,
30Debug is selected as the default configuration).
31
32Once the build has succeeded, test the resulting DLL. In a DOS
33command window, chdir to that directory. You should now be able to
34repeat the following session "(C>" is the DOS prompt, ">>>" is the
35Python prompt):
36
37 C> ..\..\vc40\python.exe
38 >>> import example
39 >>> example.foo()
40 Hello, world
41 >>>
42
43
44Creating the project
45--------------------
46
47There are two ways to use this example to create a project for your
48own module. First, choose a name ("spam" is always a winner :-) and
49create a directory for it. Copy your C sources into it. Note that
50the module source file name does not necessarily have to match the
51module name, but the "init" function name should match the module name
52-- i.e. you can only import a module "spam" if its init function is
53called "initspam()", and it should call Py_InitModule with the string
54"spam" as its first argument. By convention, it lives in a file
55called "spam.c" or "spammodule.c". The output file should be called
56"spam.dll" or "spam.pyd" (the latter is supported to avoid confusion
57with a system library "spam.dll" to which your module could be a
58Python interface).
59
60Now your options are:
61
621) Clone example.mak. Start by copying example_nt\example.mak to
63spam\spam.mak. Do a global edit on spam.mak, replacing all
64occurrences of the string "example" by "spam", and all occurrences of
65"DEP_CPP_EXAMP" by something like "DEP_CPP_SPAM". You can now use
66this makefile to create a project file by opening it as a workspace
67(you have to change the pattern to *.mak first).
68
692) Create a brand new project; instructions are below.
70
71In both cases, copy example_nt\example.def to spam\spam.def, and edit
72spam\spam.def so its second line contains the string "initspam". If
73you created a new project yourself, add the file spam.def to the
74project now.
75
76You are now all set to build your extension, unless it requires other
77external libraries, include files, etc. See Python's Extending and
78Embedding manual for instructions on how to write an extension.
79
80
81Creating a brand new project
82----------------------------
83
84If you don't feel comfortable with editing Makefiles, you can create a
85brand new project from scratch easily.
86
87Use the "File->New..." dialog to create a new Project Workspace.
88Select Dynamic-Link Library, enter the name ("spam"), and make sure
89the "Location" is set to the spam directory you have created (which
90should be a direct subdirectory of the Python build tree). Select
91Win32 as the platform (in my version, this is the only choice). Click
92"Create".
93
94Now open the "Build->Settings..." dialog. (Impressive, isn't it? :-)
95You only need to change a few settings. Make sure you have both the
96Debug and the Release configuration selected when you make the first
97change. Select the "C/C++" tab. Choose the "Preprocessor" category
98in the popup menu at the top. Type the following text in the entry
99box labeled "Addditional include directories:"
100
101 ..\Include,..\PC
102
103You must also change the run-time library. This must be done
104separately for the Release and Debug configurations. Choose the "Code
105Generation" category in the C/C++ tab. In the box labeled "Use
106run-time library", choose "Multithreaded DLL" for the Release
107configuration, and "Debug Multithreaded DLL" for the Debug
108configuration. That's all.
109
110You should now first create the file spam.def as instructed in the
111previous section.
112
113Now chose the "Insert->Files into Project..." dialog. Set the pattern
114to *.* and select both spam.c and spam.def and click OK. (Inserting
115them one by one is fine too.) Using the same dialog, choose the file
116..\vc40\python15.lib and insert it into the project.