2007-08-15 11:28:22 -03:00
|
|
|
.. highlightlang:: c
|
|
|
|
|
|
|
|
|
|
|
|
.. _building-on-windows:
|
|
|
|
|
|
|
|
****************************************
|
|
|
|
Building C and C++ Extensions on Windows
|
|
|
|
****************************************
|
|
|
|
|
|
|
|
This chapter briefly explains how to create a Windows extension module for
|
|
|
|
Python using Microsoft Visual C++, and follows with more detailed background
|
|
|
|
information on how it works. The explanatory material is useful for both the
|
|
|
|
Windows programmer learning to build Python extensions and the Unix programmer
|
|
|
|
interested in producing software which can be successfully built on both Unix
|
|
|
|
and Windows.
|
|
|
|
|
|
|
|
Module authors are encouraged to use the distutils approach for building
|
|
|
|
extension modules, instead of the one described in this section. You will still
|
|
|
|
need the C compiler that was used to build Python; typically Microsoft Visual
|
|
|
|
C++.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
This chapter mentions a number of filenames that include an encoded Python
|
|
|
|
version number. These filenames are represented with the version number shown
|
Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64722 | georg.brandl | 2008-07-05 12:13:36 +0200 (Sat, 05 Jul 2008) | 4 lines
#2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.
This is a new feature, but Barry authorized adding it in the beta period.
........
r64729 | mark.dickinson | 2008-07-05 13:33:52 +0200 (Sat, 05 Jul 2008) | 5 lines
Issue 3188: accept float('infinity') as well as float('inf'). This
makes the float constructor behave in the same way as specified
by various other language standards, including C99, IEEE 754r,
and the IBM Decimal standard.
........
r64753 | gregory.p.smith | 2008-07-06 05:35:58 +0200 (Sun, 06 Jul 2008) | 4 lines
- Issue #2862: Make int and float freelist management consistent with other
freelists. Changes their CompactFreeList apis into ClearFreeList apis and
calls them via gc.collect().
........
r64845 | raymond.hettinger | 2008-07-10 16:03:19 +0200 (Thu, 10 Jul 2008) | 1 line
Issue 3301: Bisect functions behaved badly when lo was negative.
........
r64846 | raymond.hettinger | 2008-07-10 16:34:57 +0200 (Thu, 10 Jul 2008) | 1 line
Issue 3285: Fractions from_float() and from_decimal() accept Integral arguments.
........
r64849 | andrew.kuchling | 2008-07-10 16:43:31 +0200 (Thu, 10 Jul 2008) | 1 line
Wording changes
........
r64871 | raymond.hettinger | 2008-07-11 14:00:21 +0200 (Fri, 11 Jul 2008) | 1 line
Add cautionary note on the use of PySequence_Fast_ITEMS.
........
r64880 | amaury.forgeotdarc | 2008-07-11 23:28:25 +0200 (Fri, 11 Jul 2008) | 5 lines
#3317 in zipfile module, restore the previous names of global variables:
some applications relied on them.
Also remove duplicated lines.
........
r64881 | amaury.forgeotdarc | 2008-07-11 23:45:06 +0200 (Fri, 11 Jul 2008) | 3 lines
#3342: In tracebacks, printed source lines were not indented since r62555.
#3343: Py_DisplaySourceLine should be a private function. Rename it to _Py_DisplaySourceLine.
........
r64882 | josiah.carlson | 2008-07-12 00:17:14 +0200 (Sat, 12 Jul 2008) | 2 lines
Fix for the AttributeError in test_asynchat.
........
r64885 | josiah.carlson | 2008-07-12 01:26:59 +0200 (Sat, 12 Jul 2008) | 2 lines
Fixed test for asyncore.
........
r64888 | matthias.klose | 2008-07-12 09:51:48 +0200 (Sat, 12 Jul 2008) | 2 lines
- Fix bashisms in Tools/faqwiz/move-faqwiz.sh
........
r64897 | benjamin.peterson | 2008-07-12 22:16:19 +0200 (Sat, 12 Jul 2008) | 1 line
fix various doc typos #3320
........
r64900 | alexandre.vassalotti | 2008-07-13 00:06:53 +0200 (Sun, 13 Jul 2008) | 2 lines
Fixed typo.
........
r64901 | benjamin.peterson | 2008-07-13 01:41:19 +0200 (Sun, 13 Jul 2008) | 1 line
#1778443 robotparser fixes from Aristotelis Mikropoulos
........
r64915 | nick.coghlan | 2008-07-13 16:52:36 +0200 (Sun, 13 Jul 2008) | 1 line
Fix issue 3221 by emitting a RuntimeWarning instead of raising SystemError when the parent module can't be found during an absolute import (likely due to non-PEP 361 aware code which sets a module level __package__ attribute)
........
r64926 | martin.v.loewis | 2008-07-13 22:31:49 +0200 (Sun, 13 Jul 2008) | 2 lines
Add turtle into the module index.
........
r64927 | alexandre.vassalotti | 2008-07-13 22:42:44 +0200 (Sun, 13 Jul 2008) | 3 lines
Issue #3274: Use a less common identifier for the temporary variable
in Py_CLEAR().
........
r64928 | andrew.kuchling | 2008-07-13 23:43:25 +0200 (Sun, 13 Jul 2008) | 1 line
Re-word
........
r64929 | andrew.kuchling | 2008-07-13 23:43:52 +0200 (Sun, 13 Jul 2008) | 1 line
Add various items; move ctypes items into a subsection of their own
........
r64938 | andrew.kuchling | 2008-07-14 02:35:32 +0200 (Mon, 14 Jul 2008) | 1 line
Typo fixes
........
r64939 | andrew.kuchling | 2008-07-14 02:40:55 +0200 (Mon, 14 Jul 2008) | 1 line
Typo fix
........
r64940 | andrew.kuchling | 2008-07-14 03:18:16 +0200 (Mon, 14 Jul 2008) | 1 line
Typo fix
........
r64941 | andrew.kuchling | 2008-07-14 03:18:31 +0200 (Mon, 14 Jul 2008) | 1 line
Expand the multiprocessing section
........
r64944 | gregory.p.smith | 2008-07-14 08:06:48 +0200 (Mon, 14 Jul 2008) | 7 lines
Fix posix.fork1() / os.fork1() to only call PyOS_AfterFork() in the child
process rather than both parent and child.
Does anyone actually use fork1()? It appears to be a Solaris thing
but if Python is built with pthreads on Solaris, fork1() and fork()
should be the same.
........
r64961 | jesse.noller | 2008-07-15 15:47:33 +0200 (Tue, 15 Jul 2008) | 1 line
multiprocessing/connection.py patch to remove fqdn oddness for issue 3270
........
r64966 | nick.coghlan | 2008-07-15 17:40:22 +0200 (Tue, 15 Jul 2008) | 1 line
Add missing NEWS entry for r64962
........
r64973 | jesse.noller | 2008-07-15 20:29:18 +0200 (Tue, 15 Jul 2008) | 1 line
Revert 3270 patch: self._address is in pretty widespread use, need to revisit
........
2008-07-16 09:55:28 -03:00
|
|
|
as ``XY``; in practice, ``'X'`` will be the major version number and ``'Y'``
|
2007-08-15 11:28:22 -03:00
|
|
|
will be the minor version number of the Python release you're working with. For
|
|
|
|
example, if you are using Python 2.2.1, ``XY`` will actually be ``22``.
|
|
|
|
|
|
|
|
|
|
|
|
.. _win-cookbook:
|
|
|
|
|
|
|
|
A Cookbook Approach
|
|
|
|
===================
|
|
|
|
|
|
|
|
There are two approaches to building extension modules on Windows, just as there
|
|
|
|
are on Unix: use the :mod:`distutils` package to control the build process, or
|
|
|
|
do things manually. The distutils approach works well for most extensions;
|
|
|
|
documentation on using :mod:`distutils` to build and package extension modules
|
|
|
|
is available in :ref:`distutils-index`. This section describes the manual
|
|
|
|
approach to building Python extensions written in C or C++.
|
|
|
|
|
|
|
|
To build extensions using these instructions, you need to have a copy of the
|
|
|
|
Python sources of the same version as your installed Python. You will need
|
|
|
|
Microsoft Visual C++ "Developer Studio"; project files are supplied for VC++
|
|
|
|
version 7.1, but you can use older versions of VC++. Notice that you should use
|
|
|
|
the same version of VC++that was used to build Python itself. The example files
|
|
|
|
described here are distributed with the Python sources in the
|
|
|
|
:file:`PC\\example_nt\\` directory.
|
|
|
|
|
|
|
|
#. **Copy the example files** --- The :file:`example_nt` directory is a
|
|
|
|
subdirectory of the :file:`PC` directory, in order to keep all the PC-specific
|
|
|
|
files under the same directory in the source distribution. However, the
|
|
|
|
:file:`example_nt` directory can't actually be used from this location. You
|
|
|
|
first need to copy or move it up one level, so that :file:`example_nt` is a
|
|
|
|
sibling of the :file:`PC` and :file:`Include` directories. Do all your work
|
|
|
|
from within this new location.
|
|
|
|
|
|
|
|
#. **Open the project** --- From VC++, use the :menuselection:`File --> Open
|
|
|
|
Solution` dialog (not :menuselection:`File --> Open`!). Navigate to and select
|
|
|
|
the file :file:`example.sln`, in the *copy* of the :file:`example_nt` directory
|
|
|
|
you made above. Click Open.
|
|
|
|
|
|
|
|
#. **Build the example DLL** --- In order to check that everything is set up
|
|
|
|
right, try building:
|
|
|
|
|
|
|
|
#. Select a configuration. This step is optional. Choose
|
|
|
|
:menuselection:`Build --> Configuration Manager --> Active Solution Configuration`
|
|
|
|
and select either :guilabel:`Release` or :guilabel:`Debug`. If you skip this
|
|
|
|
step, VC++ will use the Debug configuration by default.
|
|
|
|
|
|
|
|
#. Build the DLL. Choose :menuselection:`Build --> Build Solution`. This
|
|
|
|
creates all intermediate and result files in a subdirectory called either
|
|
|
|
:file:`Debug` or :file:`Release`, depending on which configuration you selected
|
|
|
|
in the preceding step.
|
|
|
|
|
|
|
|
#. **Testing the debug-mode DLL** --- Once the Debug build has succeeded, bring
|
|
|
|
up a DOS box, and change to the :file:`example_nt\\Debug` directory. You should
|
|
|
|
now be able to repeat the following session (``C>`` is the DOS prompt, ``>>>``
|
|
|
|
is the Python prompt; note that build information and various debug output from
|
|
|
|
Python may not match this screen dump exactly)::
|
|
|
|
|
|
|
|
C>..\..\PCbuild\python_d
|
|
|
|
Adding parser accelerators ...
|
|
|
|
Done.
|
|
|
|
Python 2.2 (#28, Dec 19 2001, 23:26:37) [MSC 32 bit (Intel)] on win32
|
|
|
|
Type "copyright", "credits" or "license" for more information.
|
|
|
|
>>> import example
|
|
|
|
[4897 refs]
|
|
|
|
>>> example.foo()
|
|
|
|
Hello, world
|
|
|
|
[4903 refs]
|
|
|
|
>>>
|
|
|
|
|
|
|
|
Congratulations! You've successfully built your first Python extension module.
|
|
|
|
|
|
|
|
#. **Creating your own project** --- Choose a name and create a directory for
|
|
|
|
it. Copy your C sources into it. Note that the module source file name does
|
|
|
|
not necessarily have to match the module name, but the name of the
|
|
|
|
initialization function should match the module name --- you can only import a
|
2010-10-06 07:11:56 -03:00
|
|
|
module :mod:`spam` if its initialization function is called :c:func:`initspam`,
|
|
|
|
and it should call :c:func:`Py_InitModule` with the string ``"spam"`` as its
|
2007-08-15 11:28:22 -03:00
|
|
|
first argument (use the minimal :file:`example.c` in this directory as a guide).
|
|
|
|
By convention, it lives in a file called :file:`spam.c` or :file:`spammodule.c`.
|
2009-01-03 17:18:54 -04:00
|
|
|
The output file should be called :file:`spam.pyd` (in Release mode) or
|
Merged revisions 66045,66048-66049,66053,66060,66062-66063,66065,66067,66071-66074,66080,66082-66083,66090-66093,66097-66099,66103,66105,66110,66118 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r66045 | andrew.kuchling | 2008-08-26 19:27:18 -0500 (Tue, 26 Aug 2008) | 1 line
Trim whitespace; add a few updates
........
r66048 | andrew.kuchling | 2008-08-26 19:45:02 -0500 (Tue, 26 Aug 2008) | 1 line
Add an item and a note
........
r66049 | andrew.kuchling | 2008-08-26 21:12:18 -0500 (Tue, 26 Aug 2008) | 1 line
Add various items
........
r66053 | georg.brandl | 2008-08-28 04:40:18 -0500 (Thu, 28 Aug 2008) | 2 lines
#3711: .dll isn't a valid Python extension anymore.
........
r66060 | armin.rigo | 2008-08-29 16:21:52 -0500 (Fri, 29 Aug 2008) | 3 lines
A collection of crashers, all variants of the idea
of issue #3720.
........
r66062 | georg.brandl | 2008-08-30 04:49:36 -0500 (Sat, 30 Aug 2008) | 2 lines
#3730: mention "server" attribute explicitly.
........
r66063 | georg.brandl | 2008-08-30 04:52:44 -0500 (Sat, 30 Aug 2008) | 2 lines
#3716: fix typo.
........
r66065 | georg.brandl | 2008-08-30 05:03:09 -0500 (Sat, 30 Aug 2008) | 2 lines
#3569: eval() also accepts "exec"able code objects.
........
r66067 | georg.brandl | 2008-08-30 08:17:39 -0500 (Sat, 30 Aug 2008) | 2 lines
super() actually returns a super object.
........
r66071 | andrew.kuchling | 2008-08-30 10:19:57 -0500 (Sat, 30 Aug 2008) | 1 line
Partial edits from revision and tidying pass
........
r66072 | andrew.kuchling | 2008-08-30 10:21:23 -0500 (Sat, 30 Aug 2008) | 1 line
Tidy up some sentences
........
r66073 | andrew.kuchling | 2008-08-30 10:25:47 -0500 (Sat, 30 Aug 2008) | 1 line
Correction from Antoine Pitrou: BufferedWriter and Reader support seek()
........
r66074 | andrew.kuchling | 2008-08-30 11:44:54 -0500 (Sat, 30 Aug 2008) | 1 line
Edit four more sections
........
r66080 | georg.brandl | 2008-08-30 17:00:28 -0500 (Sat, 30 Aug 2008) | 2 lines
Fix markup.
........
r66082 | andrew.kuchling | 2008-08-30 17:56:54 -0500 (Sat, 30 Aug 2008) | 1 line
More edits; markup fixes
........
r66083 | andrew.kuchling | 2008-08-30 21:24:08 -0500 (Sat, 30 Aug 2008) | 1 line
More edits
........
r66090 | andrew.kuchling | 2008-08-31 09:29:31 -0500 (Sun, 31 Aug 2008) | 1 line
Edit the library section, rearranging items to flow better and making lots of edits
........
r66091 | andrew.kuchling | 2008-08-31 10:41:48 -0500 (Sun, 31 Aug 2008) | 1 line
Last batch of edits; remove the 'other changes' section
........
r66092 | andrew.kuchling | 2008-08-31 10:48:44 -0500 (Sun, 31 Aug 2008) | 1 line
Update patch/bug count
........
r66093 | gregory.p.smith | 2008-08-31 11:34:18 -0500 (Sun, 31 Aug 2008) | 3 lines
issue3715: docstring representation of hex escaped string needs to be double
escaped.
........
r66097 | benjamin.peterson | 2008-09-01 09:13:43 -0500 (Mon, 01 Sep 2008) | 4 lines
#3703 unhelpful _fileio.FileIO error message when trying to open a directory
Reviewer: Gregory P. Smith
........
r66098 | georg.brandl | 2008-09-01 09:15:55 -0500 (Mon, 01 Sep 2008) | 2 lines
#3749: fix c'n'p errors.
........
r66099 | benjamin.peterson | 2008-09-01 09:18:30 -0500 (Mon, 01 Sep 2008) | 4 lines
Fix compilation when --without-threads is given #3683
Reviewer: Georg Brandl, Benjamin Peterson
........
r66103 | vinay.sajip | 2008-09-01 09:30:10 -0500 (Mon, 01 Sep 2008) | 1 line
logging: fixed lack of use of encoding attribute specified on a stream.
........
r66105 | vinay.sajip | 2008-09-01 09:33:59 -0500 (Mon, 01 Sep 2008) | 1 line
logging: fixed lack of use of encoding attribute specified on a stream.
........
r66110 | vinay.sajip | 2008-09-01 10:08:07 -0500 (Mon, 01 Sep 2008) | 1 line
Added section about configuring logging in a library. Thanks to Thomas Heller for the idea.
........
r66118 | vinay.sajip | 2008-09-01 12:44:14 -0500 (Mon, 01 Sep 2008) | 1 line
Bug #3738: Documentation is now more accurate in describing handler close methods.
........
2008-09-01 21:31:15 -03:00
|
|
|
:file:`spam_d.pyd` (in Debug mode). The extension :file:`.pyd` was chosen
|
|
|
|
to avoid confusion with a system library :file:`spam.dll` to which your module
|
|
|
|
could be a Python interface.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
Now your options are:
|
|
|
|
|
|
|
|
#. Copy :file:`example.sln` and :file:`example.vcproj`, rename them to
|
2010-12-28 05:29:19 -04:00
|
|
|
:file:`spam.\*`, and edit them by hand, or
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
#. Create a brand new project; instructions are below.
|
|
|
|
|
|
|
|
In either case, copy :file:`example_nt\\example.def` to :file:`spam\\spam.def`,
|
|
|
|
and edit the new :file:`spam.def` so its second line contains the string
|
|
|
|
'``initspam``'. If you created a new project yourself, add the file
|
|
|
|
:file:`spam.def` to the project now. (This is an annoying little file with only
|
|
|
|
two lines. An alternative approach is to forget about the :file:`.def` file,
|
|
|
|
and add the option :option:`/export:initspam` somewhere to the Link settings, by
|
|
|
|
manually editing the setting in Project Properties dialog).
|
|
|
|
|
|
|
|
#. **Creating a brand new project** --- Use the :menuselection:`File --> New
|
|
|
|
--> Project` dialog to create a new Project Workspace. Select :guilabel:`Visual
|
|
|
|
C++ Projects/Win32/ Win32 Project`, enter the name (``spam``), and make sure the
|
|
|
|
Location is set to parent of the :file:`spam` directory you have created (which
|
|
|
|
should be a direct subdirectory of the Python build tree, a sibling of
|
|
|
|
:file:`Include` and :file:`PC`). Select Win32 as the platform (in my version,
|
|
|
|
this is the only choice). Make sure the Create new workspace radio button is
|
|
|
|
selected. Click OK.
|
|
|
|
|
|
|
|
You should now create the file :file:`spam.def` as instructed in the previous
|
|
|
|
section. Add the source files to the project, using :menuselection:`Project -->
|
|
|
|
Add Existing Item`. Set the pattern to ``*.*`` and select both :file:`spam.c`
|
|
|
|
and :file:`spam.def` and click OK. (Inserting them one by one is fine too.)
|
|
|
|
|
|
|
|
Now open the :menuselection:`Project --> spam properties` dialog. You only need
|
|
|
|
to change a few settings. Make sure :guilabel:`All Configurations` is selected
|
|
|
|
from the :guilabel:`Settings for:` dropdown list. Select the C/C++ tab. Choose
|
|
|
|
the General category in the popup menu at the top. Type the following text in
|
|
|
|
the entry box labeled :guilabel:`Additional Include Directories`::
|
|
|
|
|
|
|
|
..\Include,..\PC
|
|
|
|
|
|
|
|
Then, choose the General category in the Linker tab, and enter ::
|
|
|
|
|
|
|
|
..\PCbuild
|
|
|
|
|
|
|
|
in the text box labelled :guilabel:`Additional library Directories`.
|
|
|
|
|
|
|
|
Now you need to add some mode-specific settings:
|
|
|
|
|
|
|
|
Select :guilabel:`Release` in the :guilabel:`Configuration` dropdown list.
|
|
|
|
Choose the :guilabel:`Link` tab, choose the :guilabel:`Input` category, and
|
|
|
|
append ``pythonXY.lib`` to the list in the :guilabel:`Additional Dependencies`
|
|
|
|
box.
|
|
|
|
|
|
|
|
Select :guilabel:`Debug` in the :guilabel:`Configuration` dropdown list, and
|
|
|
|
append ``pythonXY_d.lib`` to the list in the :guilabel:`Additional Dependencies`
|
|
|
|
box. Then click the C/C++ tab, select :guilabel:`Code Generation`, and select
|
|
|
|
:guilabel:`Multi-threaded Debug DLL` from the :guilabel:`Runtime library`
|
|
|
|
dropdown list.
|
|
|
|
|
|
|
|
Select :guilabel:`Release` again from the :guilabel:`Configuration` dropdown
|
|
|
|
list. Select :guilabel:`Multi-threaded DLL` from the :guilabel:`Runtime
|
|
|
|
library` dropdown list.
|
|
|
|
|
|
|
|
If your module creates a new type, you may have trouble with this line::
|
|
|
|
|
2009-02-27 13:11:23 -04:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-09-14 13:02:01 -03:00
|
|
|
Static type object initializers in extension modules may cause
|
|
|
|
compiles to fail with an error message like "initializer not a
|
|
|
|
constant". This shows up when building DLL under MSVC. Change it to::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-02-27 13:11:23 -04:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
and add the following to the module initialization function::
|
|
|
|
|
2010-12-28 05:29:19 -04:00
|
|
|
if (PyType_Ready(&MyObject_Type) < 0)
|
|
|
|
return NULL;
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _dynamic-linking:
|
|
|
|
|
|
|
|
Differences Between Unix and Windows
|
|
|
|
====================================
|
|
|
|
|
|
|
|
.. sectionauthor:: Chris Phoenix <cphoenix@best.com>
|
|
|
|
|
|
|
|
|
|
|
|
Unix and Windows use completely different paradigms for run-time loading of
|
|
|
|
code. Before you try to build a module that can be dynamically loaded, be aware
|
|
|
|
of how your system works.
|
|
|
|
|
|
|
|
In Unix, a shared object (:file:`.so`) file contains code to be used by the
|
|
|
|
program, and also the names of functions and data that it expects to find in the
|
|
|
|
program. When the file is joined to the program, all references to those
|
|
|
|
functions and data in the file's code are changed to point to the actual
|
|
|
|
locations in the program where the functions and data are placed in memory.
|
|
|
|
This is basically a link operation.
|
|
|
|
|
|
|
|
In Windows, a dynamic-link library (:file:`.dll`) file has no dangling
|
|
|
|
references. Instead, an access to functions or data goes through a lookup
|
|
|
|
table. So the DLL code does not have to be fixed up at runtime to refer to the
|
|
|
|
program's memory; instead, the code already uses the DLL's lookup table, and the
|
|
|
|
lookup table is modified at runtime to point to the functions and data.
|
|
|
|
|
|
|
|
In Unix, there is only one type of library file (:file:`.a`) which contains code
|
|
|
|
from several object files (:file:`.o`). During the link step to create a shared
|
|
|
|
object file (:file:`.so`), the linker may find that it doesn't know where an
|
|
|
|
identifier is defined. The linker will look for it in the object files in the
|
|
|
|
libraries; if it finds it, it will include all the code from that object file.
|
|
|
|
|
|
|
|
In Windows, there are two types of library, a static library and an import
|
|
|
|
library (both called :file:`.lib`). A static library is like a Unix :file:`.a`
|
|
|
|
file; it contains code to be included as necessary. An import library is
|
|
|
|
basically used only to reassure the linker that a certain identifier is legal,
|
|
|
|
and will be present in the program when the DLL is loaded. So the linker uses
|
|
|
|
the information from the import library to build the lookup table for using
|
|
|
|
identifiers that are not included in the DLL. When an application or a DLL is
|
|
|
|
linked, an import library may be generated, which will need to be used for all
|
|
|
|
future DLLs that depend on the symbols in the application or DLL.
|
|
|
|
|
|
|
|
Suppose you are building two dynamic-load modules, B and C, which should share
|
|
|
|
another block of code A. On Unix, you would *not* pass :file:`A.a` to the
|
|
|
|
linker for :file:`B.so` and :file:`C.so`; that would cause it to be included
|
|
|
|
twice, so that B and C would each have their own copy. In Windows, building
|
|
|
|
:file:`A.dll` will also build :file:`A.lib`. You *do* pass :file:`A.lib` to the
|
|
|
|
linker for B and C. :file:`A.lib` does not contain code; it just contains
|
|
|
|
information which will be used at runtime to access A's code.
|
|
|
|
|
|
|
|
In Windows, using an import library is sort of like using ``import spam``; it
|
|
|
|
gives you access to spam's names, but does not create a separate copy. On Unix,
|
|
|
|
linking with a library is more like ``from spam import *``; it does create a
|
|
|
|
separate copy.
|
|
|
|
|
|
|
|
|
|
|
|
.. _win-dlls:
|
|
|
|
|
|
|
|
Using DLLs in Practice
|
|
|
|
======================
|
|
|
|
|
|
|
|
.. sectionauthor:: Chris Phoenix <cphoenix@best.com>
|
|
|
|
|
|
|
|
|
|
|
|
Windows Python is built in Microsoft Visual C++; using other compilers may or
|
|
|
|
may not work (though Borland seems to). The rest of this section is MSVC++
|
|
|
|
specific.
|
|
|
|
|
|
|
|
When creating DLLs in Windows, you must pass :file:`pythonXY.lib` to the linker.
|
|
|
|
To build two DLLs, spam and ni (which uses C functions found in spam), you could
|
|
|
|
use these commands::
|
|
|
|
|
|
|
|
cl /LD /I/python/include spam.c ../libs/pythonXY.lib
|
|
|
|
cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib
|
|
|
|
|
|
|
|
The first command created three files: :file:`spam.obj`, :file:`spam.dll` and
|
|
|
|
:file:`spam.lib`. :file:`Spam.dll` does not contain any Python functions (such
|
2010-10-06 07:11:56 -03:00
|
|
|
as :c:func:`PyArg_ParseTuple`), but it does know how to find the Python code
|
2007-08-15 11:28:22 -03:00
|
|
|
thanks to :file:`pythonXY.lib`.
|
|
|
|
|
|
|
|
The second command created :file:`ni.dll` (and :file:`.obj` and :file:`.lib`),
|
|
|
|
which knows how to find the necessary functions from spam, and also from the
|
|
|
|
Python executable.
|
|
|
|
|
|
|
|
Not every identifier is exported to the lookup table. If you want any other
|
|
|
|
modules (including Python) to be able to see your identifiers, you have to say
|
|
|
|
``_declspec(dllexport)``, as in ``void _declspec(dllexport) initspam(void)`` or
|
|
|
|
``PyObject _declspec(dllexport) *NiGetSpamData(void)``.
|
|
|
|
|
|
|
|
Developer Studio will throw in a lot of import libraries that you do not really
|
|
|
|
need, adding about 100K to your executable. To get rid of them, use the Project
|
|
|
|
Settings dialog, Link tab, to specify *ignore default libraries*. Add the
|
|
|
|
correct :file:`msvcrtxx.lib` to the list of libraries.
|
|
|
|
|