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