⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 123.html

📁 Python Ebook Python&XML
💻 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 -&gt; SWIG桾he Simple Wrapper Interface Generator</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> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="117.html" class="navtitle">6. Extending and Embedding Python</a> &gt; <span class="nonavtitle">SWIG桾he Simple Wrapper Interface Generator</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="122.html" title="Compiling and Linking Extension Modules"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=123" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="123.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="124.html" title="Other Wrappers"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A35%3A51+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162147033230210041176116136</font><a href="read3.asp?bookname=0672319942&snode=123&now=5%2F31%2F2002+4%3A35%3A51+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>SWIG桾he Simple Wrapper Interface Generator</h3>
				<p><i>SWIG (Simple Wrapper and Interface Generator)</I> is an automated tool create by <A NAme="idx1073744042"></a>David Beazley used to write interfaces between Python and existing C libraries. These interfaces can contain several single functions.</p>

				<P>The programmer doesn't have to write any special wrapping functions to provide the glue between the Python scripting language and the C functions.</P>

				<P>SWIG works by reading an interface file that contains function and method prototypes. It automatically does the necessary type conversion, checks the code for error, produces a C file, compiles the file, and builds it into a shared object file.</P>

				<p>It works by taking the declarations commonly found in C/C++ header files and using them to generate the <a naME="idx1073744043"></A><A name="idx1073744044"></a>glue code (wrappers) that scripting languages need to access the underlying C/C++ code.<a name="idx1073744045"></a><a name="idx1073744046"></a><a naMe="idx1073744047"></a><A namE="idx1073744048"></a><a naMe="idx1073744049"></a><a NAME="idx1073744050"></a><a naME="idx1073744051"></A><A name="idx1073744052"></A><A NAme="idx1073744053"></a><a NAME="idx1073744054"></a><a name="idx1073744055"></a><a name="idx1073744056"></a><a name="idx1073744057"></a></p>

				<P>SWIG is better suited as a mechanism for controlling a variety of C programs because it enables someone to combine bits and pieces of completely different software packages without waiting for someone else to write a special purpose module.</p>

				<p>The handling of datatypes when using SWIG for prototyping and control application is very easy because whenever SWIG finds an unknown datatype, it simply assumes that it is some kind of complex datatype. Consequently, wrapping a complex C program doesn't imply too much work.</P>

				<p>SWIG provides a convenient way of building Python interfaces to libraries.</p>

				<p>You just need to write simple interface definitions, which SWIG uses to generate the C program that conforms to the Python/C extension guidelines.</P>

				<p>SWIG makes it even easier to use scripting languages by automating the process of connecting scripting languages to C/C++ code.</p>

				<p>Many reasons you should try SWIG are as follows:</p>

				<BlocKQUOte>
<p><p>You can easily replace the <TT CLass="monofont">main()</tT> function of a C program with Python's interpreter.</P>
</P>
<P><p>C/C++ code is easily tested because you can call C functions and libraries directly from your scripting environment.</p>
</p>
<p><P>Debugging your C code also becomes easier once you use Python's interpreter. Remember that you don't need to change your C code in order to use SWIG.</P>
</P>
<P><p>SWIG can integrate different C/C++ programs together by turning them into extension modules. After the extensions are created, Python can combine and use them to generate new applications.</p>
</p>
<p><p>SWIG understands and parses ANSI C/C++ syntax.</p>
</p>
<p><p>The output of SWIG is a fully functional scripting language module.<a name="idx1073744058"></a><a name="idx1073744059"></A><a nAme="idx1073744060"></a><A name="idx1073744061"></A><a naME="idx1073744062"></A><A name="idx1073744063"></A><A NAme="idx1073744064"></a><a NAME="idx1073744065"></a><a naME="idx1073744066"></A><A name="idx1073744067"></a><a name="idx1073744068"></a><a name="idx1073744069"></a><a naMe="idx1073744070"></a></P>
</p>
<p><p>As SWIG is designed to work with existing C/C++ code, it will be rarely necessary to change your existing programs.</P>
</p>
<p><p>Your C/C++ code remains separate from your Python code.</p>
</P>
<p><p>SWIG output can be freely extended and customized.</p>
</P>
<P><P>Now, the most interesting thing is that you don't need to master all the details about the Python/C API in order to use the basics of SWIG to create your Python extension modules. SWIG automates the process of generating a Python extension based on the header of the functions that you want to export.</P>
</p>
</bloCKQUote>
				<p>Take a look at the following example and see how simple it is to generate a wrapper file. We will first create an input file, and call it <TT CLass="monofont">helloworld.i.</tT></P>

				<PRe>
					
// file: helloworld.i

%module helloworld
%{
#include "helloworld.h"
%}

char *say();

				</pre>

				<p>Now, we will use SWIG to <a name="idx1073744071"></a><a name="idx1073744072"></a><a naMe="idx1073744073"></a>generate the wrapper file. We need to pass an argument to SWIG informing that the wrapper must be created for the Python language. That's because SWIG works with many different languages.<A namE="idx1073744074"></a><a naMe="idx1073744075"></a><a NAME="idx1073744076"></a><a naME="idx1073744077"></A><A name="idx1073744078"></A><A NAme="idx1073744079"></a><a NAME="idx1073744080"></a><a name="idx1073744081"></a><a name="idx1073744082"></a><a name="idx1073744083"></a><a NamE="idx1073744084"></a><a nAme="idx1073744085"></a><a Name="idx1073744086"></A></P>

				<PRe>
					
% swig -python helloworld.i
<i>Generating wrappers for Python

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -