📄 122.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> Compiling and Linking Extension Modules</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="117.html" class="navtitle">6. Extending and Embedding Python</a> > <span class="nonavtitle">Compiling and Linking Extension Modules</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="121.html" title="Extending"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=122" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="122.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="123.html" title="SWIG桾he Simple Wrapper Interface Generator"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A35%3A24+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147033230213142232249029</font><a href="read3.asp?bookname=0672319942&snode=122&now=5%2F31%2F2002+4%3A35%3A24+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>Compiling and Linking Extension Modules</h3>
<p>Two options are available for building Python extension modules. The first one compiles and links the module into the interpreter. This option makes the module always available to the interpreter.</p>
<P>The second option doesn't require that you recompile the interpreter because it dynamically links the modules to the system.</P>
<H4>Linking Static Extensions to the Interpreter</H4>
<p>Before starting, make sure that you have already compiled the interpreter's source code (refer to <a hrEF="227.html">Chapter 17, "Development Tools,"</A> for more details). Building and installing Python before adding new modules is essential to have the libraries and other files in the right places.</P>
<h5>Static Extensions on UNIX</h5>
<p>On UNIX, Python modules written in C are easily identified by looking at the <tT CLAss="monofont">/usr/lib/Python1.5</tt> directory. Most of the time, they are the shared library files with the <tt class="monofont">.so</tt> extension. Although, if you are using HPUX, the extension is <tt class="monofont">.sl,</Tt> and on some others it is just <tT claSs="monofont">.o.</tt></p>
<P>The next few steps show how to create static extensions on UNIX.<a naME="idx1073743891"></A><A name="idx1073743892"></A><A NAme="idx1073743893"></a><a NAME="idx1073743894"></a><a naME="idx1073743895"></A><A name="idx1073743896"></a><a name="idx1073743897"></a><a name="idx1073743898"></a><a naMe="idx1073743899"></a><A namE="idx1073743900"></a><a naMe="idx1073743901"></a><a NAME="idx1073743902"></a><a naME="idx1073743903"></A><A name="idx1073743904"></A><A NAme="idx1073743905"></a><a NAME="idx1073743906"></a><a name="idx1073743907"></a><a name="idx1073743908"></a><a name="idx1073743909"></a></p>
<P>
<p>
<dL>
<dd>
<b>Step 1. </B>
<p>You need to copy your module to the <tt cLass="monofont">Modules</TT> directory.</P>
</Dd>
<dd>
<b>Step 2. </B>
<P>You have to add the following entry to the end of the <TT clasS="monofont">/modules/Setup.in</TT> configuration file, which is located in the Python source tree. This file has the list of all the external libraries needed by the interpreter.</P>
<pre>
*static*
modulename filename
</pRE>
</DD>
</dl>
<p>For example,</p>
<pre>
hello /mnt/hda/python/helloworldmodule.c
</pre>
<p>If your extension module requires additional libraries, add the argument <tt class="monofont">-l</tt><I><tt ClasS="monofont">libraryname</tt></i> at the end of the line.</p>
<P>For example,</p>
<prE>
hello /mnt/hda/python/helloworldmodule.c -l/mnt/hda/python/auxmodule.c
</PRE>
<p>The <a naME="idx1073743910"></A><A name="idx1073743911"></A><TT Class="monofont">*static*</TT> flag builds the modules as static modules. The other option is to use the <A Name="idx1073743912"></a><a name="idx1073743913"></a><tt class="monofont">*shared*</tt> flag, which means that they have to be built as shared modules (known as <tT clAss="monofont">DLL</tT>s on Windows).</p>
<p>The last step is to recompile Python as normal to include the extra module by typing <b><tT claSS="monofont">./configure</TT></b> and <b><tt CLASs="monofont">make</tt></b> in the top of the Python Source tree. The Python interpreter is rebuilt after that.</P>
<P>To execute the new interpreter and test your new extension module, just call it like this:</P>
<Pre>
./python<a nAME="idx1073743914"></A>
</pre>
<h5>Static Extensions on Windows</h5>
<p>The following instructions are based on the use of Microsoft Visual C++ version 5.</p>
<p>First, you need to inform Python's <tt class="monofont">include path.</tt> To do that, go to Tools, Options, Directories (see <a hrEf="122#4.html">Figure 6.2</a>).<A namE="idx1073743915"></a><a naMe="idx1073743916"></a><a NAME="idx1073743917"></a><a naME="idx1073743918"></A><A name="idx1073743919"></A><A NAme="idx1073743920"></a><a NAME="idx1073743921"></a><a name="idx1073743922"></a><a name="idx1073743923"></a><a name="idx1073743924"></a><a NamE="idx1073743925"></a><a nAme="idx1073743926"></a><a Name="idx1073743927"></A><A NAme="idx1073743928"></a><a NAME="idx1073743929"></a><a naME="idx1073743930"></A><A name="idx1073743931"></A><A NAme="idx1073743932"></a></p>
<center>
<h5>
<a name="4"></a>Figure 6.2. You need to inform the include path.</h5><imG boRder="0" Width="500" HeigHT="375" SRc="graphics/06fig02.gif" alt="graphics/06fig02.gif"></CENTer>
<p>It is also necessary to inform the library's location (see <a HREF="122#5.html">Figure 6.3</a>). You need to add the <tt cLASS="monofont">python15.lib</tt> directory to your Tools, Options, Directories, Library files.</p>
<center><h5>
<a name="5"></a>Figure 6.3. You need to inform the python15.lib path.</h5><img BorDer="0" wIdth="500" hEighT="375" SRC="graphics/06fig03.gif" alt="graphics/06fig03.gif"></cENTEr>
<p>Now, the rest is easy.</p>
<oL TYPe="1">
<li>
<p>Using a text editor, open the <TT CLass="monofont">\PC\config.c</tt> file.</p>
</li>
<li>
<p>Look for the first comment. You need to add an external reference to the <tt class="monofont">init</tt> function of your module.</P>
<prE>
/* -- ADDMODULE MARKER 1 -- */
extern void init<i>modulename</i>();
</pRe>
</li>
<lI>
<p>Locate the next comment. You need to add the module name and the init function.</p>
<pRE>
/* -- ADDMODULE MARKER 2 -- */
{"modulename", init<I>modulename</I>},
</pre>
</lI>
<LI>
<P>Using a text editor, open the <tt clASS="monofont">/PCbuild/python15.dsp</Tt> file.</p>
</li>
<LI>
<P>Go to the end of the file. Locate the entry that references the <Tt class="monofont">yuvconvert.c</tt> source file. You need to add the location of your module's source file just before that entry.<a name="idx1073743933"></a><a name="idx1073743934"></a><A naMe="idx1073743935"></a><a Name="idx1073743936"></a><A namE="idx1073743937"></A><A Name="idx1073743938"></a><A NAMe="idx1073743939"></a><a nAME="idx1073743940"></A><a namE="idx1073743941"></A><A Name="idx1073743942"></a><a name="idx1073743943"></a><a name="idx1073743944"></a><a namE="idx1073743945"></a><a Name="idx1073743946"></A></p>
<pre>
SOURCE=..\Modules\<B><i>yourmodulename</i></b>module.c
# End Source File
# Begin Source File
SOURCE=..\Modules\yuvconvert.c
# End Source File
# End Target
# End Project
</PRE>
</Li>
<li>
<p>Using Microsoft Visual C++, open the <TT CLass="monofont">/PCbuild/pcbuild.dsw</tT> workspace.</P>
</LI>
<li>
<p>Select the <tT CLAss="monofont">Batch Build</tt> option and say <tt class="monofont">Rebuild All.</tt></p>
</li>
</ol>
<p>By default, the <tT clAss="monofont">EXE</tT> file and the <tt clAss="monofont">DLL</tT>s will be saved in your <TT Class="monofont">/Pcbuild/</TT> directory.<A Name="idx1073743947"></a><A NAMe="idx1073743948"></a><a nAME="idx1073743949"></A><a name="idx1073743950"></a><a name="idx1073743951"></a></p>
<h4>Linking Dynamic Extensions to the Interpreter</h4>
<p>Now look at what you should do in order to create dynamic extension modules.</p>
<h5>Dynamic Extensions on UNIX</h5>
<p>The next few steps show how to build Dynamic extensions on UNIX.</P>
<p>
<p>
<P>
<dl>
<dD>
<b>Step 1. </b>
<p>Put the reference to your module in the <tT claSS="monofont">Setup.in</TT> file. If your module references other source files, you should include them too. You might want to create a new <tt clASS="monofont">Setup.in</Tt> file in the directory containing your module.</p>
<prE>
*shared*
spam helloworldmodule.c
</PRE>
</dd>
<dd>
<B>Step 2. </B>
<P>Copy the <Tt class="monofont">Makefile.pre.in</tt> file to the directory where your module is located.<a name="idx1073743952"></a>
<a name="idx1073743953"></a><A naMe="idx1073743954"></a><a Name="idx1073743955"></a><A namE="idx1073743956"></A><A Name="idx1073743957"></a><A NAMe="idx1073743958"></a><a nAME="idx1073743959"></A><a namE="idx1073743960"></A><A Name="idx1073743961"></a><a name="idx1073743962"></a><a name="idx1073743963"></a><a namE="idx1073743964"></a><a Name="idx1073743965"></A><a namE="idx1073743966"></a><a nAME="idx1073743967"></A><a namE="idx1073743968"></A><A Name="idx1073743969"></a></P>
</DD>
<Dd>
<b>Step 3. </b>
<p>Type</P>
<PRE>
make -f Makefile.pre.in boot
make
</pre>
</dd>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -