blob: 2487ae5ea2861af9ab4b0b4006095b1ee4bd7e14 [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
Guido van Rossum55b8b031997-12-11 04:01:25 +00005extension module using Microsoft VC++ ("Developer Studio") version 4.x
6or 5.x, except for the Python distribution. It has been tested with
7VC++ 4.2 on Python 1.5a3, and with VC++ 5.0 on Python 1.5b2.
Guido van Rossum29168ce1997-05-16 16:17:20 +00008
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
Guido van Rossum55b8b031997-12-11 04:01:25 +000017When using the VC++ 4.x project (makefile), it is assumed that the
18build results of Python are in the directory ..\vc40. In particular,
19the python15.lib file is referred to as "..\vc40\python15.lib". If
20you have problems with this file, the best thing to do is to delete it
21from the project and add it again.
Guido van Rossum29168ce1997-05-16 16:17:20 +000022
Guido van Rossum55b8b031997-12-11 04:01:25 +000023When using the VC++ 5.x project (workspace), the build results of
24Python are assumed to be in ..\PCbuild. Since the provided VC++ 5.x
25project and workspace files have a different structure (to support
26separate "release" and "debug" builds), the example project and
27workspace match this structure.
28
29In order to use the example project from VC++ 4.x, use the "File->Open
30Workspace..." dialog (*not* the "File->Open..." dialog!). Change the
31pattern to "*.mak" and select the file "example.mak". Now choose
32"File->Save All" and the othe project files will be created.
33
34From VC+ 5.x, do the same except don't change the pattern, and select
35the example.dsw workspace file.
Guido van Rossum29168ce1997-05-16 16:17:20 +000036
37In order to check that everything is set up right, try building:
38choose "Build->Build example.dll". This creates all intermediate and
39result files in a subdirectory which is called either Debug or Release
Guido van Rossum55b8b031997-12-11 04:01:25 +000040depending on which configuration you have chosen.
Guido van Rossum29168ce1997-05-16 16:17:20 +000041
42Once the build has succeeded, test the resulting DLL. In a DOS
43command window, chdir to that directory. You should now be able to
44repeat the following session "(C>" is the DOS prompt, ">>>" is the
45Python prompt):
46
47 C> ..\..\vc40\python.exe
48 >>> import example
49 >>> example.foo()
50 Hello, world
51 >>>
52
Guido van Rossum55b8b031997-12-11 04:01:25 +000053When using VC++ 5.x, issue these commands:
54
55 C> ..\..\PCbuild\Release\python.exe
56 >>> import example
57 >>> example.foo()
58 Hello, world
59 >>>
60
Guido van Rossum29168ce1997-05-16 16:17:20 +000061
62Creating the project
63--------------------
64
65There are two ways to use this example to create a project for your
66own module. First, choose a name ("spam" is always a winner :-) and
67create a directory for it. Copy your C sources into it. Note that
68the module source file name does not necessarily have to match the
69module name, but the "init" function name should match the module name
70-- i.e. you can only import a module "spam" if its init function is
71called "initspam()", and it should call Py_InitModule with the string
72"spam" as its first argument. By convention, it lives in a file
73called "spam.c" or "spammodule.c". The output file should be called
74"spam.dll" or "spam.pyd" (the latter is supported to avoid confusion
75with a system library "spam.dll" to which your module could be a
76Python interface).
77
78Now your options are:
79
801) Clone example.mak. Start by copying example_nt\example.mak to
81spam\spam.mak. Do a global edit on spam.mak, replacing all
82occurrences of the string "example" by "spam", and all occurrences of
83"DEP_CPP_EXAMP" by something like "DEP_CPP_SPAM". You can now use
84this makefile to create a project file by opening it as a workspace
Guido van Rossum55b8b031997-12-11 04:01:25 +000085(you have to change the pattern to *.mak first). (When using VC++
865.x, you can clone example.dsp and example.dsw in a similar way.)
Guido van Rossum29168ce1997-05-16 16:17:20 +000087
882) Create a brand new project; instructions are below.
89
90In both cases, copy example_nt\example.def to spam\spam.def, and edit
91spam\spam.def so its second line contains the string "initspam". If
92you created a new project yourself, add the file spam.def to the
Guido van Rossum55b8b031997-12-11 04:01:25 +000093project now. (This is an annoying little file with only two lines.
94An alternative approach is to forget about the .def file, and add the
95option "/export:initspam" somewhere to the Link settings, by manually
96editing the "Project Options" box).
Guido van Rossum29168ce1997-05-16 16:17:20 +000097
98You are now all set to build your extension, unless it requires other
99external libraries, include files, etc. See Python's Extending and
100Embedding manual for instructions on how to write an extension.
101
102
103Creating a brand new project
104----------------------------
105
Guido van Rossum55b8b031997-12-11 04:01:25 +0000106If you don't feel comfortable with editing Makefiles or project and
107workspace files, you can create a brand new project from scratch
108easily.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000109
110Use the "File->New..." dialog to create a new Project Workspace.
111Select Dynamic-Link Library, enter the name ("spam"), and make sure
112the "Location" is set to the spam directory you have created (which
113should be a direct subdirectory of the Python build tree). Select
114Win32 as the platform (in my version, this is the only choice). Click
115"Create".
116
117Now open the "Build->Settings..." dialog. (Impressive, isn't it? :-)
118You only need to change a few settings. Make sure you have both the
119Debug and the Release configuration selected when you make the first
120change. Select the "C/C++" tab. Choose the "Preprocessor" category
121in the popup menu at the top. Type the following text in the entry
122box labeled "Addditional include directories:"
123
124 ..\Include,..\PC
125
Guido van Rossum55b8b031997-12-11 04:01:25 +0000126Next, for both configurations, select the "Link" tab, choose the
127"General" category, and add "python15.lib" to the end of the
128"Object/library modules" box.
129
130Then, separately for the Release and Debug configurations, choose the
131"Input" category in the Link tab, and enter "..\PCbuild\Release" or
132"..\PCbuild\Debug", respectively, in the "Additional library path"
133box.
134
135Finally, you must change the run-time library. This must also be done
Guido van Rossum29168ce1997-05-16 16:17:20 +0000136separately for the Release and Debug configurations. Choose the "Code
137Generation" category in the C/C++ tab. In the box labeled "Use
138run-time library", choose "Multithreaded DLL" for the Release
139configuration, and "Debug Multithreaded DLL" for the Debug
140configuration. That's all.
141
142You should now first create the file spam.def as instructed in the
143previous section.
144
145Now chose the "Insert->Files into Project..." dialog. Set the pattern
146to *.* and select both spam.c and spam.def and click OK. (Inserting
Guido van Rossum55b8b031997-12-11 04:01:25 +0000147them one by one is fine too.)
148