/PC/example_nt/readme.txt

http://unladen-swallow.googlecode.com/ · Plain Text · 183 lines · 146 code · 37 blank · 0 comment · 0 complexity · 9abd7004b881b855e6bfa61e1a17bd83 MD5 · raw file

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