blob: aa2b3091909343434f3fc77d1b4ae6b1dcbd9665 [file] [log] [blame]
Guido van Rossum29168ce1997-05-16 16:17:20 +00001Example Python extension for Windows NT
2=======================================
3
Tim Peters090c2162000-07-02 23:18:43 +00004This directory contains everything needed (except for the Python
5distribution!) to build a Python extension module using Microsoft VC++
6("Developer Studio") version 6. It has been tested with VC++ 6.0 on Python
Tim Petersfc1a7ce2001-12-15 22:27:01 +000072.2c1. You can also use earlier versions of VC to build Python extensions,
Tim Peters090c2162000-07-02 23:18:43 +00008but the sample VC project file (example.dsw in this directory) is in VC 6
9format.
Guido van Rossum29168ce1997-05-16 16:17:20 +000010
Tim Peters090c2162000-07-02 23:18:43 +000011COPY THIS DIRECTORY!
Guido van Rossum29168ce1997-05-16 16:17:20 +000012--------------------
Tim Peters090c2162000-07-02 23:18:43 +000013This "example_nt" directory is a subdirectory of the PC directory, in order
14to keep all the PC-specific files under the same directory. However, the
15example_nt directory can't actually be used from this location. You first
16need to copy or move it up one level, so that example_nt is a direct
17sibling of the PC\ and Include\ directories. Do all your work from within
18this new location -- sorry, but you'll be sorry if you don't.
Guido van Rossum29168ce1997-05-16 16:17:20 +000019
Tim Peters090c2162000-07-02 23:18:43 +000020OPEN THE PROJECT
21----------------
22From VC 6.x, use the
23 File -> Open Workspace...
24dialog (*not* the "File -> Open..." dialog!). Navigate to and select the
Tim Petersa2bf2702001-01-19 08:45:48 +000025file "example.dsw", in the *copy* of the example_nt directory you made
26above.
27Click Open.
Tim Peters090c2162000-07-02 23:18:43 +000028
29BUILD THE EXAMPLE DLL
30---------------------
31In order to check that everything is set up right, try building:
32
331. Select a configuration. This step is optional. Do
34 Build -> Select Active Configuration...
35 and select either "example - Win32 Release" or "example - Win32 Debug".
36 If you skip this step, you'll use the Debug configuration by default.
37
382. Build the DLL. Do
39 Build -> Build example_d.dll
40 in Debug mode, or
41 Build -> Build example.dll
42 in Release mode.
43 This creates all intermediate and result files in a subdirectory which
44 is called either Debug or Release, depending on which configuration you
45 picked in the preceding step.
46
47TESTING THE DEBUG-MODE DLL
48--------------------------
49Once the Debug build has succeeded, bring up a DOS box, and cd to
50example_nt\Debug. You should now be able to repeat the following session
51("C>" is the DOS prompt, ">>>" is the Python prompt) (note that various
52debug output from Python may not match this screen dump exactly):
53
54 C>..\..\PCbuild\python_d
55 Adding parser accelerators ...
56 Done.
Tim Petersfc1a7ce2001-12-15 22:27:01 +000057 Python 2.2c1+ (#28, Dec 14 2001, 18:06:39) [MSC 32 bit (Intel)] on win32
58 Type "help", "copyright", "credits" or "license" for more information.
Tim Peters090c2162000-07-02 23:18:43 +000059 >>> import example
Tim Petersfc1a7ce2001-12-15 22:27:01 +000060 [7052 refs]
Tim Peters090c2162000-07-02 23:18:43 +000061 >>> example.foo()
62 Hello, world
Tim Petersfc1a7ce2001-12-15 22:27:01 +000063 [7052 refs]
Tim Peters090c2162000-07-02 23:18:43 +000064 >>>
65
66TESTING THE RELEASE-MODE DLL
67----------------------------
68Once the Release build has succeeded, bring up a DOS box, and cd to
69example_nt\Release. You should now be able to repeat the following session
70("C>" is the DOS prompt, ">>>" is the Python prompt):
71
72 C>..\..\PCbuild\python
Tim Petersfc1a7ce2001-12-15 22:27:01 +000073 Python 2.2c1+ (#28, Dec 14 2001, 18:06:04) [MSC 32 bit (Intel)] on win32
74 Type "help", "copyright", "credits" or "license" for more information.
Tim Peters090c2162000-07-02 23:18:43 +000075 >>> import example
76 >>> example.foo()
77 Hello, world
78 >>>
79
80Congratulations! You've successfully built your first Python extension
81module.
82
83CREATING YOUR OWN PROJECT
84-------------------------
85Choose a name ("spam" is always a winner :-) and create a directory for
86it. Copy your C sources into it. Note that the module source file name
87does not necessarily have to match the module name, but the "init" function
88name should match the module name -- i.e. you can only import a module
89"spam" if its init function is called "initspam()", and it should call
90Py_InitModule with the string "spam" as its first argument (use the minimal
91example.c in this directory as a guide). By convention, it lives in a file
Guido van Rossum29168ce1997-05-16 16:17:20 +000092called "spam.c" or "spammodule.c". The output file should be called
Tim Peters090c2162000-07-02 23:18:43 +000093"spam.dll" or "spam.pyd" (the latter is supported to avoid confusion with a
94system library "spam.dll" to which your module could be a Python interface)
95in Release mode, or spam_d.dll or spam_d.pyd in Debug mode.
Guido van Rossum29168ce1997-05-16 16:17:20 +000096
97Now your options are:
98
Tim Peters090c2162000-07-02 23:18:43 +0000991) Copy example.dsw and example.dsp, rename them to spam.*, and edit them
100by hand.
101
102or
Guido van Rossum29168ce1997-05-16 16:17:20 +0000103
1042) Create a brand new project; instructions are below.
105
Tim Peters090c2162000-07-02 23:18:43 +0000106In either case, copy example_nt\example.def to spam\spam.def, and edit the
107new spam.def so its second line contains the string "initspam". If you
108created a new project yourself, add the file spam.def to the project now.
109(This is an annoying little file with only two lines. An alternative
110approach is to forget about the .def file, and add the option
111"/export:initspam" somewhere to the Link settings, by manually editing the
112"Project Options" box).
Guido van Rossum29168ce1997-05-16 16:17:20 +0000113
114You are now all set to build your extension, unless it requires other
115external libraries, include files, etc. See Python's Extending and
116Embedding manual for instructions on how to write an extension.
117
118
Tim Peters090c2162000-07-02 23:18:43 +0000119CREATING A BRAND NEW PROJECT
Guido van Rossum29168ce1997-05-16 16:17:20 +0000120----------------------------
Tim Peters090c2162000-07-02 23:18:43 +0000121Use the
122 File -> New... -> Projects
123dialog to create a new Project Workspace. Select "Win32 Dynamic-Link
124Library", enter the name ("spam"), and make sure the "Location" is set to
125the spam directory you have created (which should be a direct subdirectory
Tim Petersa2bf2702001-01-19 08:45:48 +0000126of the Python build tree, a sibling of Include and PC). Select Win32 as the
Tim Peters090c2162000-07-02 23:18:43 +0000127platform (in my version, this is the only choice). Make sure the "Create
128new workspace" radio button is selected. Click OK.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000129
Tim Peters090c2162000-07-02 23:18:43 +0000130Now open the
131 Project -> Settings...
132dialog. (Impressive, isn't it? :-) You only need to change a few
133settings. Make sure "All Configurations" is selected from the "Settings
134for:" dropdown list. Select the "C/C++" tab. Choose the "Preprocessor"
135category in the popup menu at the top. Type the following text in the
136entry box labeled "Addditional include directories:"
Guido van Rossum29168ce1997-05-16 16:17:20 +0000137
Tim Peters090c2162000-07-02 23:18:43 +0000138 ..\Include,..\PC
Guido van Rossum29168ce1997-05-16 16:17:20 +0000139
Tim Peters090c2162000-07-02 23:18:43 +0000140Then, choose the "Input" category in the Link tab, and enter
141 ..\PCbuild
142in the "Additional library path:" box.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000143
Tim Peters090c2162000-07-02 23:18:43 +0000144Now you need to add some mode-specific settings:
Guido van Rossum29168ce1997-05-16 16:17:20 +0000145
Tim Peters090c2162000-07-02 23:18:43 +0000146Select "Win32 Release" in the "Settings for:" dropdown list. Click the
Tim Petersfc1a7ce2001-12-15 22:27:01 +0000147"Link" tab, choose the "Input" Category, and append "python22.lib" to the
Tim Peters090c2162000-07-02 23:18:43 +0000148list in the "Object/library modules:" box.
Guido van Rossum55b8b031997-12-11 04:01:25 +0000149
Tim Peters090c2162000-07-02 23:18:43 +0000150Select "Win32 Debug" in the "Settings for:" dropdown list, and append
Tim Petersfc1a7ce2001-12-15 22:27:01 +0000151"python22_d.lib" to the list in the "Object/library modules:" box. Then
Tim Peters090c2162000-07-02 23:18:43 +0000152click on the C/C++ tab, select "Code Generation" from the "Category:"
153dropdown list, and select "Debug Multithreaded DLL" from the "Use run-time
154library:" dropdown list.
Guido van Rossum55b8b031997-12-11 04:01:25 +0000155
Tim Peters090c2162000-07-02 23:18:43 +0000156Select "Win32 Release" again from the "Settings for:" dropdown list.
157Select "Multithreaded DLL" from the "Use run-time library:" dropdown list.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000158
Tim Peters090c2162000-07-02 23:18:43 +0000159That's all <wink>.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000160
Tim Peters090c2162000-07-02 23:18:43 +0000161You should now create the file spam.def as instructed in the previous
162section. Then chose the
163 Insert -> Files into Project...
164dialog. Set the pattern to *.* and select both spam.c and spam.def and
165click OK. (Inserting them one by one is fine too.)