Fixes for HTML to validate

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@6231 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2004-09-06 21:37:00 +00:00
commit 8f6341859e
30 changed files with 265 additions and 355 deletions

View file

@ -1,14 +1,14 @@
SWIG Tutorial
<p>
<img src="images/tutorial.png">
<img src="images/tutorial.png" alt="Tutorial">
<p>
So you want to get going in a hurry? To illustrate the use of SWIG,
suppose you have some C functions you want added to Tcl, Perl, Python, Java and C#.
Specifically, let's say you have them in a file 'example.c'
<ul><tt><pre>
<blockquote><pre>
/* File : example.c */
#include &lt;time.h&gt;
@ -30,7 +30,7 @@ char *get_time()
return ctime(&amp;ltime);
}
</pre></tt></ul>
</pre></blockquote>
<h3>Interface file </h3>
@ -38,7 +38,7 @@ Now, in order to add these files to your favorite language, you need to write an
"interface file" which is the input to SWIG. An interface file for these
C functions might look like this :
<ul><tt><pre>
<blockquote><pre>
/* example.i */
%module example
%{
@ -49,14 +49,14 @@ extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
</pre></tt></ul>
</pre></blockquote>
<h3> Building a Tcl module </h3>
At the UNIX prompt, type the following (shown for Linux, see the <a href="http://swig.cs.uchicago.edu/cgi-bin/wiki.pl?SwigFaq/SharedLibraries">SWIG Wiki Shared Libraries</a> page for help with other operating systems):
<blockquote>
<pre><tt>
<pre>
unix % swig -tcl example.i
unix % gcc -fpic -c example.c example_wrap.c \
-I/usr/local/include
@ -73,7 +73,7 @@ unix % tclsh
Sun Feb 11 23:01:07 1996
%
</tt></pre></blockquote>
</pre></blockquote>
The <tt> swig </tt> command produces a file <a href = "tutorial/example_wrap.html">
<tt> example_wrap.c </tt> </a> that should be compiled and linked with
@ -85,31 +85,31 @@ the 'load' command.
Turning C code into a Python module is also easy. Simply do the following (shown for Irix, see the <a href="http://swig.cs.uchicago.edu/cgi-bin/wiki.pl?SwigFaq/SharedLibraries">SWIG Wiki Shared Libraries</a> page for help with other operating systems):
<blockquote> <tt> <pre>
<blockquote> <pre>
unix % swig -python example.i
unix % gcc -c example.c example_wrap.c \
-I/usr/local/include/python2.1
unix % ld -shared example.o example_wrap.o -o _example.so
</pre> </tt> </blockquote>
</pre> </blockquote>
We can now use the Python module as follows :
<blockquote> <tt> <pre>
>>> import example
>>> example.fact(5)
<blockquote> <pre>
&gt;&gt;&gt; import example
&gt;&gt;&gt; example.fact(5)
120
>>> example.my_mod(7,3)
&gt;&gt;&gt; example.my_mod(7,3)
1
>>> example.get_time()
&gt;&gt;&gt; example.get_time()
'Sun Feb 11 23:01:07 1996'
>>>
&gt;&gt;&gt;
</pre>
</tt> </blockquote>
</blockquote>
<h3> Building a Perl module </h3>
You can also build a Perl5 module as follows (shown for Solaris, see the <a href="http://swig.cs.uchicago.edu/cgi-bin/wiki.pl?SwigFaq/SharedLibraries">SWIG Wiki Shared Libraries</a> page for help with other operating systems):
<blockquote><tt><pre>
<blockquote><pre>
unix % swig -perl5 example.i
unix % gcc -c example.c example_wrap.c \
-I/usr/lib/perl/solaris/5.003/CORE
@ -124,12 +124,12 @@ print example::get_time(),"\n";
120
Sun Feb 11 23:01:07 1996
unix %
</pre></tt></blockquote>
</pre></blockquote>
<h3> Building a Java module </h3>
SWIG will also generate JNI code for accessing C/C++ code from Java. Here is an example building a Java module (shown for Cygwin, see the <a href="http://swig.cs.uchicago.edu/cgi-bin/wiki.pl?SwigFaq/SharedLibraries">SWIG Wiki Shared Libraries</a> page for help with other operating systems):
<blockquote><tt><pre>
<blockquote><pre>
$ swig -java example.i
$ gcc -c example.c example_wrap.c -I/c/jdk1.3.1/include -I/c/jdk1.3.1/include/win32
$ gcc -shared example.o example_wrap.o -mno-cygwin -Wl,--add-stdcall-alias -o example.dll
@ -148,12 +148,12 @@ $ java main
120
Mon Mar 4 18:20:31 2002
$
</pre></tt></blockquote>
</pre></blockquote>
<h3> Building a C# module </h3>
SWIG will also generate code for accessing C/C++ code from C# using PInvoke. Here is an example building a C# module (shown for Linux, see the <a href="http://swig.cs.uchicago.edu/cgi-bin/wiki.pl?SwigFaq/SharedLibraries">SWIG Wiki Shared Libraries</a> page for help with other operating systems). It uses the open source <a href="http://www.southern-storm.com.au/portable_net.html">DotGNU Portable.NET</a> C# compiler which runs on most Unix systems, but works equally well using other C# compilers:
<blockquote><tt><pre>
<blockquote><pre>
$ swig -csharp example.i
$ gcc -c -fpic example.c example_wrap.c
$ gcc -shared example.o example_wrap.o -o libexample.so
@ -173,7 +173,7 @@ $ ilrun runme
Tue May 13 10:45:45 2003
$
</pre></tt></blockquote>
</pre></blockquote>
<h3> SWIG for the truly lazy </h3>
@ -182,7 +182,7 @@ file. If you have a header file, you can often just include it directly in the
SWIG interface. For example:
<blockquote>
<tt> <pre>
<pre>
%module example
%{
/* Includes the header in the wrapper code */
@ -191,7 +191,7 @@ SWIG interface. For example:
/* Parse the header file to generate wrappers */
%include "header.h"
</pre></tt>
</pre>
</blockquote>
Alternatively, some people might just include SWIG directives in a header
@ -254,7 +254,7 @@ namespace std {
T1 first;
T2 second;
pair() : first(T1()), second(T2()) { };
pair(const T1 &f, const T2 &s) : first(f), second(s) { }
pair(const T1 &amp;f, const T2 &amp;s) : first(f), second(s) { }
};
}
</pre>
@ -294,19 +294,19 @@ $ python
Python 2.1 (#3, Aug 20 2001, 15:41:42)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.
>>> import pair
>>> a = pair.pairii(3,4)
>>> a.first
&gt;&gt;&gt; import pair
&gt;&gt;&gt; a = pair.pairii(3,4)
&gt;&gt;&gt; a.first
3
>>> a.second
&gt;&gt;&gt; a.second
4
>>> a.second = 16
>>> a.second
&gt;&gt;&gt; a.second = 16
&gt;&gt;&gt; a.second
16
>>> b = pair.pairdi(3.5,8)
>>> b.first
&gt;&gt;&gt; b = pair.pairdi(3.5,8)
&gt;&gt;&gt; b.first
3.5
>>> b.second
&gt;&gt;&gt; b.second
8
</pre>
</blockquote>