Tim Peters | fe8a566 | 2006-03-01 06:28:58 +0000 | [diff] [blame] | 1 | Example Python extension for Windows NT |
| 2 | ======================================= |
| 3 | |
| 4 | This directory contains everything needed (except for the Python |
| 5 | distribution!) to build a Python extension module using Microsoft VC++ |
| 6 | ("Developer Studio") version 7.1. It has been tested with VC++ 7.1 on |
| 7 | Python 2.4. You can also use earlier versions of VC to build Python |
| 8 | extensions, but the sample VC project file (example.dsw in this directory) |
| 9 | is in VC 7.1 format. Notice that you need to use the same compiler version |
| 10 | that was used to build Python itself. |
| 11 | |
| 12 | COPY THIS DIRECTORY! |
| 13 | -------------------- |
| 14 | This "example_nt" directory is a subdirectory of the PC directory, in order |
| 15 | to keep all the PC-specific files under the same directory. However, the |
| 16 | example_nt directory can't actually be used from this location. You first |
| 17 | need to copy or move it up one level, so that example_nt is a direct |
| 18 | sibling of the PC\ and Include\ directories. Do all your work from within |
| 19 | this new location -- sorry, but you'll be sorry if you don't. |
| 20 | |
| 21 | OPEN THE PROJECT |
| 22 | ---------------- |
| 23 | From VC 7.1, use the |
| 24 | File -> Open Solution... |
| 25 | dialog (*not* the "File -> Open..." dialog!). Navigate to and select the |
| 26 | file "example.sln", in the *copy* of the example_nt directory you made |
| 27 | above. |
| 28 | Click Open. |
| 29 | |
| 30 | BUILD THE EXAMPLE DLL |
| 31 | --------------------- |
| 32 | In order to check that everything is set up right, try building: |
| 33 | |
| 34 | 1. Select a configuration. This step is optional. Do |
| 35 | Build -> Configuration Manager... -> Active Solution Configuration |
| 36 | and select either "Release" or "Debug". |
| 37 | If you skip this step, you'll use the Debug configuration by default. |
| 38 | |
| 39 | 2. Build the DLL. Do |
| 40 | Build -> Build Solution |
| 41 | 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 | |
| 45 | TESTING THE DEBUG-MODE DLL |
| 46 | -------------------------- |
| 47 | Once the Debug build has succeeded, bring up a DOS box, and cd to |
| 48 | example_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 |
| 50 | debug output from Python may not match this screen dump exactly): |
| 51 | |
| 52 | C>..\..\PCbuild\python_d |
| 53 | Adding parser accelerators ... |
| 54 | Done. |
| 55 | 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. |
| 57 | >>> import example |
| 58 | [7052 refs] |
| 59 | >>> example.foo() |
| 60 | Hello, world |
| 61 | [7052 refs] |
| 62 | >>> |
| 63 | |
| 64 | TESTING THE RELEASE-MODE DLL |
| 65 | ---------------------------- |
| 66 | Once the Release build has succeeded, bring up a DOS box, and cd to |
| 67 | example_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 |
| 71 | 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. |
| 73 | >>> import example |
| 74 | >>> example.foo() |
| 75 | Hello, world |
| 76 | >>> |
| 77 | |
| 78 | Congratulations! You've successfully built your first Python extension |
| 79 | module. |
| 80 | |
| 81 | CREATING YOUR OWN PROJECT |
| 82 | ------------------------- |
| 83 | Choose a name ("spam" is always a winner :-) and create a directory for |
| 84 | it. Copy your C sources into it. Note that the module source file name |
| 85 | does not necessarily have to match the module name, but the "init" function |
| 86 | name 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 |
| 88 | Py_InitModule with the string "spam" as its first argument (use the minimal |
| 89 | example.c in this directory as a guide). By convention, it lives in a file |
| 90 | called "spam.c" or "spammodule.c". The output file should be called |
| 91 | "spam.dll" or "spam.pyd" (the latter is supported to avoid confusion with a |
| 92 | system library "spam.dll" to which your module could be a Python interface) |
| 93 | in Release mode, or spam_d.dll or spam_d.pyd in Debug mode. |
| 94 | |
| 95 | Now your options are: |
| 96 | |
| 97 | 1) Copy example.sln and example.vcproj, rename them to spam.*, and edit them |
| 98 | by hand. |
| 99 | |
| 100 | or |
| 101 | |
| 102 | 2) Create a brand new project; instructions are below. |
| 103 | |
| 104 | In either case, copy example_nt\example.def to spam\spam.def, and edit the |
| 105 | new spam.def so its second line contains the string "initspam". If you |
| 106 | created 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 |
| 108 | approach is to forget about the .def file, and add the option |
| 109 | "/export:initspam" somewhere to the Link settings, by manually editing the |
| 110 | "Project -> Properties -> Linker -> Command Line -> Additional Options" |
| 111 | box). |
| 112 | |
| 113 | You are now all set to build your extension, unless it requires other |
| 114 | external libraries, include files, etc. See Python's Extending and |
| 115 | Embedding manual for instructions on how to write an extension. |
| 116 | |
| 117 | |
| 118 | CREATING A BRAND NEW PROJECT |
| 119 | ---------------------------- |
| 120 | Use the |
| 121 | File -> New -> Project... |
| 122 | dialog to create a new Project Workspace. Select "Visual C++ Projects/Win32/ |
| 123 | Win32 Project", enter the name ("spam"), and make sure the "Location" is |
| 124 | set to parent of the spam directory you have created (which should be a direct |
| 125 | subdirectory of the Python build tree, a sibling of Include and PC). |
| 126 | In "Application Settings", select "DLL", and "Empty Project". Click OK. |
| 127 | |
| 128 | You should now create the file spam.def as instructed in the previous |
| 129 | section. Add the source files (including the .def file) to the project, |
| 130 | using "Project", "Add Existing Item". |
| 131 | |
| 132 | Now open the |
| 133 | Project -> spam properties... |
| 134 | dialog. (Impressive, isn't it? :-) You only need to change a few |
| 135 | settings. Make sure "All Configurations" is selected from the "Settings |
| 136 | for:" dropdown list. Select the "C/C++" tab. Choose the "General" |
| 137 | category in the popup menu at the top. Type the following text in the |
| 138 | entry box labeled "Addditional Include Directories:" |
| 139 | |
| 140 | ..\Include,..\PC |
| 141 | |
| 142 | Then, choose the "General" category in the "Linker" tab, and enter |
| 143 | ..\PCbuild |
| 144 | in the "Additional library Directories" box. |
| 145 | |
| 146 | Now you need to add some mode-specific settings (select "Accept" |
| 147 | when asked to confirm your changes): |
| 148 | |
| 149 | Select "Release" in the "Configuration" dropdown list. Click the |
| 150 | "Link" tab, choose the "Input" Category, and append "python24.lib" to the |
| 151 | list in the "Additional Dependencies" box. |
| 152 | |
| 153 | Select "Debug" in the "Settings for:" dropdown list, and append |
| 154 | "python24_d.lib" to the list in the Additional Dependencies" box. Then |
| 155 | click on the C/C++ tab, select "Code Generation", and select |
| 156 | "Multi-threaded Debug DLL" from the "Runtime library" dropdown list. |
| 157 | |
| 158 | Select "Release" again from the "Settings for:" dropdown list. |
| 159 | Select "Multi-threaded DLL" from the "Use run-time library:" dropdown list. |
| 160 | |
| 161 | That's all <wink>. |