blob: cf491299edf51e70b9c41d6d22abffafb3f1850f [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 Petersa2bf2702001-01-19 08:45:48 +000072.1a1. 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 Petersa2bf2702001-01-19 08:45:48 +000057 2.1a1 (#9, Jan 17 2001, 23:26:42) [MSC 32 bit (Intel)] on win32
Tim Peters090c2162000-07-02 23:18:43 +000058 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
59 Copyright 1995-2000 Corporation for National Research Initiatives (CNRI)
60 >>> import example
Tim Petersa2bf2702001-01-19 08:45:48 +000061 [4897 refs]
Tim Peters090c2162000-07-02 23:18:43 +000062 >>> example.foo()
63 Hello, world
Tim Petersa2bf2702001-01-19 08:45:48 +000064 [4903 refs]
Tim Peters090c2162000-07-02 23:18:43 +000065 >>>
66
67TESTING THE RELEASE-MODE DLL
68----------------------------
69Once the Release build has succeeded, bring up a DOS box, and cd to
70example_nt\Release. You should now be able to repeat the following session
71("C>" is the DOS prompt, ">>>" is the Python prompt):
72
73 C>..\..\PCbuild\python
Tim Petersa2bf2702001-01-19 08:45:48 +000074 Python 2.1a1 (#9, Jan 17 2001, 23:26:37) [MSC 32 bit (Intel)] on win32
75 Type "copyright", "credits" or "license" for more information.
Tim Peters090c2162000-07-02 23:18:43 +000076 >>> import example
77 >>> example.foo()
78 Hello, world
79 >>>
80
81Congratulations! You've successfully built your first Python extension
82module.
83
84CREATING YOUR OWN PROJECT
85-------------------------
86Choose a name ("spam" is always a winner :-) and create a directory for
87it. Copy your C sources into it. Note that the module source file name
88does not necessarily have to match the module name, but the "init" function
89name should match the module name -- i.e. you can only import a module
90"spam" if its init function is called "initspam()", and it should call
91Py_InitModule with the string "spam" as its first argument (use the minimal
92example.c in this directory as a guide). By convention, it lives in a file
Guido van Rossum29168ce1997-05-16 16:17:20 +000093called "spam.c" or "spammodule.c". The output file should be called
Tim Peters090c2162000-07-02 23:18:43 +000094"spam.dll" or "spam.pyd" (the latter is supported to avoid confusion with a
95system library "spam.dll" to which your module could be a Python interface)
96in Release mode, or spam_d.dll or spam_d.pyd in Debug mode.
Guido van Rossum29168ce1997-05-16 16:17:20 +000097
98Now your options are:
99
Tim Peters090c2162000-07-02 23:18:43 +00001001) Copy example.dsw and example.dsp, rename them to spam.*, and edit them
101by hand.
102
103or
Guido van Rossum29168ce1997-05-16 16:17:20 +0000104
1052) Create a brand new project; instructions are below.
106
Tim Peters090c2162000-07-02 23:18:43 +0000107In either case, copy example_nt\example.def to spam\spam.def, and edit the
108new spam.def so its second line contains the string "initspam". If you
109created a new project yourself, add the file spam.def to the project now.
110(This is an annoying little file with only two lines. An alternative
111approach is to forget about the .def file, and add the option
112"/export:initspam" somewhere to the Link settings, by manually editing the
113"Project Options" box).
Guido van Rossum29168ce1997-05-16 16:17:20 +0000114
115You are now all set to build your extension, unless it requires other
116external libraries, include files, etc. See Python's Extending and
117Embedding manual for instructions on how to write an extension.
118
119
Tim Peters090c2162000-07-02 23:18:43 +0000120CREATING A BRAND NEW PROJECT
Guido van Rossum29168ce1997-05-16 16:17:20 +0000121----------------------------
Tim Peters090c2162000-07-02 23:18:43 +0000122Use the
123 File -> New... -> Projects
124dialog to create a new Project Workspace. Select "Win32 Dynamic-Link
125Library", enter the name ("spam"), and make sure the "Location" is set to
126the spam directory you have created (which should be a direct subdirectory
Tim Petersa2bf2702001-01-19 08:45:48 +0000127of the Python build tree, a sibling of Include and PC). Select Win32 as the
Tim Peters090c2162000-07-02 23:18:43 +0000128platform (in my version, this is the only choice). Make sure the "Create
129new workspace" radio button is selected. Click OK.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000130
Tim Peters090c2162000-07-02 23:18:43 +0000131Now open the
132 Project -> Settings...
133dialog. (Impressive, isn't it? :-) You only need to change a few
134settings. Make sure "All Configurations" is selected from the "Settings
135for:" dropdown list. Select the "C/C++" tab. Choose the "Preprocessor"
136category in the popup menu at the top. Type the following text in the
137entry box labeled "Addditional include directories:"
Guido van Rossum29168ce1997-05-16 16:17:20 +0000138
Tim Peters090c2162000-07-02 23:18:43 +0000139 ..\Include,..\PC
Guido van Rossum29168ce1997-05-16 16:17:20 +0000140
Tim Peters090c2162000-07-02 23:18:43 +0000141Then, choose the "Input" category in the Link tab, and enter
142 ..\PCbuild
143in the "Additional library path:" box.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000144
Tim Peters090c2162000-07-02 23:18:43 +0000145Now you need to add some mode-specific settings:
Guido van Rossum29168ce1997-05-16 16:17:20 +0000146
Tim Peters090c2162000-07-02 23:18:43 +0000147Select "Win32 Release" in the "Settings for:" dropdown list. Click the
Tim Peters97c96402001-01-17 23:23:13 +0000148"Link" tab, choose the "Input" Category, and append "python21.lib" to the
Tim Peters090c2162000-07-02 23:18:43 +0000149list in the "Object/library modules:" box.
Guido van Rossum55b8b031997-12-11 04:01:25 +0000150
Tim Peters090c2162000-07-02 23:18:43 +0000151Select "Win32 Debug" in the "Settings for:" dropdown list, and append
Tim Peters97c96402001-01-17 23:23:13 +0000152"python21_d.lib" to the list in the "Object/library modules:" box. Then
Tim Peters090c2162000-07-02 23:18:43 +0000153click on the C/C++ tab, select "Code Generation" from the "Category:"
154dropdown list, and select "Debug Multithreaded DLL" from the "Use run-time
155library:" dropdown list.
Guido van Rossum55b8b031997-12-11 04:01:25 +0000156
Tim Peters090c2162000-07-02 23:18:43 +0000157Select "Win32 Release" again from the "Settings for:" dropdown list.
158Select "Multithreaded DLL" from the "Use run-time library:" dropdown list.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000159
Tim Peters090c2162000-07-02 23:18:43 +0000160That's all <wink>.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000161
Tim Peters090c2162000-07-02 23:18:43 +0000162You should now create the file spam.def as instructed in the previous
163section. Then chose the
164 Insert -> Files into Project...
165dialog. Set the pattern to *.* and select both spam.c and spam.def and
166click OK. (Inserting them one by one is fine too.)