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