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