blob: 37a9c29cafc96a15b519bd85cb17f1879149981b [file] [log] [blame]
Tim Petersfe8a5662006-03-01 06:28:58 +00001Example Python extension for Windows NT
2=======================================
3
4This directory contains everything needed (except for the Python
5distribution!) to build a Python extension module using Microsoft VC++
6("Developer Studio") version 7.1. It has been tested with VC++ 7.1 on
7Python 2.4. You can also use earlier versions of VC to build Python
8extensions, but the sample VC project file (example.dsw in this directory)
9is in VC 7.1 format. Notice that you need to use the same compiler version
10that was used to build Python itself.
11
12COPY THIS DIRECTORY!
13--------------------
14This "example_nt" directory is a subdirectory of the PC directory, in order
15to keep all the PC-specific files under the same directory. However, the
16example_nt directory can't actually be used from this location. You first
17need to copy or move it up one level, so that example_nt is a direct
18sibling of the PC\ and Include\ directories. Do all your work from within
19this new location -- sorry, but you'll be sorry if you don't.
20
21OPEN THE PROJECT
22----------------
23From VC 7.1, use the
24 File -> Open Solution...
25dialog (*not* the "File -> Open..." dialog!). Navigate to and select the
26file "example.sln", in the *copy* of the example_nt directory you made
27above.
28Click Open.
29
30BUILD THE EXAMPLE DLL
31---------------------
32In order to check that everything is set up right, try building:
33
341. Select a configuration. This step is optional. Do
35 Build -> Configuration Manager... -> Active Solution Configuration
36 and select either "Release" or "Debug".
37 If you skip this step, you'll use the Debug configuration by default.
38
392. Build the DLL. Do
40 Build -> Build Solution
41 This creates all intermediate and result files in a subdirectory which
42 is called either Debug or Release, depending on which configuration you
43 picked in the preceding step.
44
45TESTING THE DEBUG-MODE DLL
46--------------------------
47Once the Debug build has succeeded, bring up a DOS box, and cd to
48example_nt\Debug. You should now be able to repeat the following session
49("C>" is the DOS prompt, ">>>" is the Python prompt) (note that various
50debug output from Python may not match this screen dump exactly):
51
52 C>..\..\PCbuild\python_d
53 Adding parser accelerators ...
54 Done.
55 Python 2.2c1+ (#28, Dec 14 2001, 18:06:39) [MSC 32 bit (Intel)] on win32
56 Type "help", "copyright", "credits" or "license" for more information.
57 >>> import example
58 [7052 refs]
59 >>> example.foo()
60 Hello, world
61 [7052 refs]
62 >>>
63
64TESTING THE RELEASE-MODE DLL
65----------------------------
66Once the Release build has succeeded, bring up a DOS box, and cd to
67example_nt\Release. You should now be able to repeat the following session
68("C>" is the DOS prompt, ">>>" is the Python prompt):
69
70 C>..\..\PCbuild\python
71 Python 2.2c1+ (#28, Dec 14 2001, 18:06:04) [MSC 32 bit (Intel)] on win32
72 Type "help", "copyright", "credits" or "license" for more information.
73 >>> import example
74 >>> example.foo()
75 Hello, world
76 >>>
77
78Congratulations! You've successfully built your first Python extension
79module.
80
81CREATING YOUR OWN PROJECT
82-------------------------
83Choose a name ("spam" is always a winner :-) and create a directory for
84it. Copy your C sources into it. Note that the module source file name
85does not necessarily have to match the module name, but the "init" function
86name should match the module name -- i.e. you can only import a module
87"spam" if its init function is called "initspam()", and it should call
88Py_InitModule with the string "spam" as its first argument (use the minimal
89example.c in this directory as a guide). By convention, it lives in a file
90called "spam.c" or "spammodule.c". The output file should be called
91"spam.dll" or "spam.pyd" (the latter is supported to avoid confusion with a
92system library "spam.dll" to which your module could be a Python interface)
93in Release mode, or spam_d.dll or spam_d.pyd in Debug mode.
94
95Now your options are:
96
971) Copy example.sln and example.vcproj, rename them to spam.*, and edit them
98by hand.
99
100or
101
1022) Create a brand new project; instructions are below.
103
104In either case, copy example_nt\example.def to spam\spam.def, and edit the
105new spam.def so its second line contains the string "initspam". If you
106created a new project yourself, add the file spam.def to the project now.
107(This is an annoying little file with only two lines. An alternative
108approach is to forget about the .def file, and add the option
109"/export:initspam" somewhere to the Link settings, by manually editing the
110"Project -> Properties -> Linker -> Command Line -> Additional Options"
111box).
112
113You are now all set to build your extension, unless it requires other
114external libraries, include files, etc. See Python's Extending and
115Embedding manual for instructions on how to write an extension.
116
117
118CREATING A BRAND NEW PROJECT
119----------------------------
120Use the
121 File -> New -> Project...
122dialog to create a new Project Workspace. Select "Visual C++ Projects/Win32/
123Win32 Project", enter the name ("spam"), and make sure the "Location" is
124set to parent of the spam directory you have created (which should be a direct
125subdirectory of the Python build tree, a sibling of Include and PC).
126In "Application Settings", select "DLL", and "Empty Project". Click OK.
127
128You should now create the file spam.def as instructed in the previous
129section. Add the source files (including the .def file) to the project,
130using "Project", "Add Existing Item".
131
132Now open the
133 Project -> spam properties...
134dialog. (Impressive, isn't it? :-) You only need to change a few
135settings. Make sure "All Configurations" is selected from the "Settings
136for:" dropdown list. Select the "C/C++" tab. Choose the "General"
137category in the popup menu at the top. Type the following text in the
138entry box labeled "Addditional Include Directories:"
139
140 ..\Include,..\PC
141
142Then, choose the "General" category in the "Linker" tab, and enter
143 ..\PCbuild
144in the "Additional library Directories" box.
145
146Now you need to add some mode-specific settings (select "Accept"
147when asked to confirm your changes):
148
149Select "Release" in the "Configuration" dropdown list. Click the
150"Link" tab, choose the "Input" Category, and append "python24.lib" to the
151list in the "Additional Dependencies" box.
152
153Select "Debug" in the "Settings for:" dropdown list, and append
154"python24_d.lib" to the list in the Additional Dependencies" box. Then
155click on the C/C++ tab, select "Code Generation", and select
156"Multi-threaded Debug DLL" from the "Runtime library" dropdown list.
157
158Select "Release" again from the "Settings for:" dropdown list.
159Select "Multi-threaded DLL" from the "Use run-time library:" dropdown list.
160
161That's all <wink>.