blob: 65665badfff2acee9898a246cbb5797710015dec [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++
Martin v. Löwisa2cc2692004-12-29 14:15:58 +00006("Developer Studio") version 7.1. It has been tested with VC++ 7.1 on
7Python 2.4. You can also use earlier versions of VC to build Python
8extensions, but the sample VC project file (example.dsw in this directory)
9is in VC 7.1 format. Notice that you need to use the same compiler version
10that was used to build Python itself.
Guido van Rossum29168ce1997-05-16 16:17:20 +000011
Tim Peters090c2162000-07-02 23:18:43 +000012COPY THIS DIRECTORY!
Guido van Rossum29168ce1997-05-16 16:17:20 +000013--------------------
Tim Peters090c2162000-07-02 23:18:43 +000014This "example_nt" directory is a subdirectory of the PC directory, in order
15to keep all the PC-specific files under the same directory. However, the
16example_nt directory can't actually be used from this location. You first
17need to copy or move it up one level, so that example_nt is a direct
18sibling of the PC\ and Include\ directories. Do all your work from within
19this new location -- sorry, but you'll be sorry if you don't.
Guido van Rossum29168ce1997-05-16 16:17:20 +000020
Tim Peters090c2162000-07-02 23:18:43 +000021OPEN THE PROJECT
22----------------
Martin v. Löwisa2cc2692004-12-29 14:15:58 +000023From VC 7.1, use the
24 File -> Open Solution...
Tim Peters090c2162000-07-02 23:18:43 +000025dialog (*not* the "File -> Open..." dialog!). Navigate to and select the
Martin v. Löwisa2cc2692004-12-29 14:15:58 +000026file "example.sln", in the *copy* of the example_nt directory you made
Tim Petersa2bf2702001-01-19 08:45:48 +000027above.
28Click Open.
Tim Peters090c2162000-07-02 23:18:43 +000029
30BUILD THE EXAMPLE DLL
31---------------------
32In order to check that everything is set up right, try building:
33
341. Select a configuration. This step is optional. Do
Martin v. Löwisa2cc2692004-12-29 14:15:58 +000035 Build -> Configuration Manager... -> Active Solution Configuration
36 and select either "Release" or "Debug".
Tim Peters090c2162000-07-02 23:18:43 +000037 If you skip this step, you'll use the Debug configuration by default.
38
392. Build the DLL. Do
Martin v. Löwisa2cc2692004-12-29 14:15:58 +000040 Build -> Build Solution
Tim Peters090c2162000-07-02 23:18:43 +000041 This creates all intermediate and result files in a subdirectory which
42 is called either Debug or Release, depending on which configuration you
43 picked in the preceding step.
44
45TESTING THE DEBUG-MODE DLL
46--------------------------
47Once the Debug build has succeeded, bring up a DOS box, and cd to
48example_nt\Debug. You should now be able to repeat the following session
49("C>" is the DOS prompt, ">>>" is the Python prompt) (note that various
50debug output from Python may not match this screen dump exactly):
51
52 C>..\..\PCbuild\python_d
53 Adding parser accelerators ...
54 Done.
Tim Petersfc1a7ce2001-12-15 22:27:01 +000055 Python 2.2c1+ (#28, Dec 14 2001, 18:06:39) [MSC 32 bit (Intel)] on win32
56 Type "help", "copyright", "credits" or "license" for more information.
Tim Peters090c2162000-07-02 23:18:43 +000057 >>> import example
Tim Petersfc1a7ce2001-12-15 22:27:01 +000058 [7052 refs]
Tim Peters090c2162000-07-02 23:18:43 +000059 >>> example.foo()
60 Hello, world
Tim Petersfc1a7ce2001-12-15 22:27:01 +000061 [7052 refs]
Tim Peters090c2162000-07-02 23:18:43 +000062 >>>
63
64TESTING THE RELEASE-MODE DLL
65----------------------------
66Once the Release build has succeeded, bring up a DOS box, and cd to
67example_nt\Release. You should now be able to repeat the following session
68("C>" is the DOS prompt, ">>>" is the Python prompt):
69
70 C>..\..\PCbuild\python
Tim Petersfc1a7ce2001-12-15 22:27:01 +000071 Python 2.2c1+ (#28, Dec 14 2001, 18:06:04) [MSC 32 bit (Intel)] on win32
72 Type "help", "copyright", "credits" or "license" for more information.
Tim Peters090c2162000-07-02 23:18:43 +000073 >>> import example
74 >>> example.foo()
75 Hello, world
76 >>>
77
78Congratulations! You've successfully built your first Python extension
79module.
80
81CREATING YOUR OWN PROJECT
82-------------------------
83Choose a name ("spam" is always a winner :-) and create a directory for
84it. Copy your C sources into it. Note that the module source file name
85does not necessarily have to match the module name, but the "init" function
86name should match the module name -- i.e. you can only import a module
87"spam" if its init function is called "initspam()", and it should call
88Py_InitModule with the string "spam" as its first argument (use the minimal
89example.c in this directory as a guide). By convention, it lives in a file
Guido van Rossum29168ce1997-05-16 16:17:20 +000090called "spam.c" or "spammodule.c". The output file should be called
Tim Peters090c2162000-07-02 23:18:43 +000091"spam.dll" or "spam.pyd" (the latter is supported to avoid confusion with a
92system library "spam.dll" to which your module could be a Python interface)
93in Release mode, or spam_d.dll or spam_d.pyd in Debug mode.
Guido van Rossum29168ce1997-05-16 16:17:20 +000094
95Now your options are:
96
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000971) Copy example.sln and example.vcproj, rename them to spam.*, and edit them
Tim Peters090c2162000-07-02 23:18:43 +000098by hand.
99
100or
Guido van Rossum29168ce1997-05-16 16:17:20 +0000101
1022) Create a brand new project; instructions are below.
103
Tim Peters090c2162000-07-02 23:18:43 +0000104In either case, copy example_nt\example.def to spam\spam.def, and edit the
105new spam.def so its second line contains the string "initspam". If you
106created a new project yourself, add the file spam.def to the project now.
107(This is an annoying little file with only two lines. An alternative
108approach is to forget about the .def file, and add the option
109"/export:initspam" somewhere to the Link settings, by manually editing the
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000110"Project -> Properties -> Linker -> Command Line -> Additional Options"
111box).
Guido van Rossum29168ce1997-05-16 16:17:20 +0000112
113You are now all set to build your extension, unless it requires other
114external libraries, include files, etc. See Python's Extending and
115Embedding manual for instructions on how to write an extension.
116
117
Tim Peters090c2162000-07-02 23:18:43 +0000118CREATING A BRAND NEW PROJECT
Guido van Rossum29168ce1997-05-16 16:17:20 +0000119----------------------------
Tim Peters090c2162000-07-02 23:18:43 +0000120Use the
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000121 File -> New -> Project...
122dialog to create a new Project Workspace. Select "Visual C++ Projects/Win32/
123Win32 Project", enter the name ("spam"), and make sure the "Location" is
124set to parent of the spam directory you have created (which should be a direct
125subdirectory of the Python build tree, a sibling of Include and PC).
126In "Application Settings", select "DLL", and "Empty Project". Click OK.
127
128You should now create the file spam.def as instructed in the previous
129section. Add the source files (including the .def file) to the project,
130using "Project", "Add Existing Item".
Guido van Rossum29168ce1997-05-16 16:17:20 +0000131
Tim Peters090c2162000-07-02 23:18:43 +0000132Now open the
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000133 Project -> spam properties...
Tim Peters090c2162000-07-02 23:18:43 +0000134dialog. (Impressive, isn't it? :-) You only need to change a few
135settings. Make sure "All Configurations" is selected from the "Settings
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000136for:" dropdown list. Select the "C/C++" tab. Choose the "General"
Tim Peters090c2162000-07-02 23:18:43 +0000137category in the popup menu at the top. Type the following text in the
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000138entry box labeled "Addditional Include Directories:"
Guido van Rossum29168ce1997-05-16 16:17:20 +0000139
Tim Peters090c2162000-07-02 23:18:43 +0000140 ..\Include,..\PC
Guido van Rossum29168ce1997-05-16 16:17:20 +0000141
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000142Then, choose the "General" category in the "Linker" tab, and enter
Tim Peters090c2162000-07-02 23:18:43 +0000143 ..\PCbuild
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000144in the "Additional library Directories" box.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000145
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000146Now you need to add some mode-specific settings (select "Accept"
147when asked to confirm your changes):
Guido van Rossum29168ce1997-05-16 16:17:20 +0000148
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000149Select "Release" in the "Configuration" dropdown list. Click the
150"Link" tab, choose the "Input" Category, and append "python24.lib" to the
151list in the "Additional Dependencies" box.
Guido van Rossum55b8b031997-12-11 04:01:25 +0000152
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000153Select "Debug" in the "Settings for:" dropdown list, and append
154"python24_d.lib" to the list in the Additional Dependencies" box. Then
155click on the C/C++ tab, select "Code Generation", and select
156"Multi-threaded Debug DLL" from the "Runtime library" dropdown list.
Guido van Rossum55b8b031997-12-11 04:01:25 +0000157
Martin v. Löwisa2cc2692004-12-29 14:15:58 +0000158Select "Release" again from the "Settings for:" dropdown list.
159Select "Multi-threaded DLL" from the "Use run-time library:" dropdown list.
Guido van Rossum29168ce1997-05-16 16:17:20 +0000160
Tim Peters090c2162000-07-02 23:18:43 +0000161That's all <wink>.