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