Java tutorial added. Also typo fix as suggested by Jim Avera.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@2419 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2002-03-04 21:50:20 +00:00
commit 616f62dbb5

View file

@ -5,7 +5,7 @@ SWIG 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, and Python.
suppose you have some C functions you want added to Tcl, Perl, Python and Java.
Specifically, let's say you have them in a file 'example.c'
<ul><tt><pre>
@ -34,7 +34,7 @@ char *get_time()
<h3> Interface file </h3>
Now, in order to add these files to your favorite scripting language, you need to write an
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 :
@ -58,7 +58,6 @@ At the UNIX prompt, type the following (shown for Linux):
<blockquote>
<pre><tt>
unix % swig -tcl example.i
Making wrappers for Tcl
unix % gcc -fpic -c example.c example_wrap.c \
-I/usr/local/include
unix % gcc -shared example.o example_wrap.o -o example.so
@ -110,7 +109,6 @@ Turning C code into a Python module is also easy. Simply do the following (show
<blockquote> <tt> <pre>
unix % swig -python example.i
Making wrappers for Python
unix % gcc -c example.c example_wrap.c \
-I/usr/local/include/python1.4 \
-I/usr/local/lib/python1.4/config
@ -131,11 +129,10 @@ We can now use the Python module as follows :
</tt> </blockquote>
<h3> Building a Perl module </h3>
Finally, you can build a Perl5 module as follows (shown for Solaris):
You can also build a Perl5 module as follows (shown for Solaris):
<blockquote><tt><pre>
unix % swig -perl5 example.i
Making wrappers for Perl5
unix % gcc -c example.c example_wrap.c \
-I/usr/lib/perl/solaris/5.003/CORE
unix % ld -G example.o example_wrap.o -o example.so
@ -143,7 +140,7 @@ unix % perl
use example;
print $example::My_variable,"\n";
print example::fact(5),"\n";
print example.get_time(),"\n";
print example::get_time(),"\n";
&lt;ctrl-d&gt;
3.0
120
@ -152,6 +149,32 @@ unix %
</pre></tt></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):
<blockquote><tt><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 -Wl,--add-stdcall-alias -o example.dll
$ cat main.java
import example;
public class main {
public static void main(String argv[]) {
System.loadLibrary("example");
System.out.println(example.get_My_variable());
System.out.println(example.fact(5));
System.out.println(example.get_time());
}
}
$ javac main.java
$ java main
3.0
120
Mon Mar 4 18:20:31 2002
$
</pre></tt></blockquote>
<h3> SWIG for the truly lazy </h3>
As it turns out, it is not always necessary to write a special interface
@ -167,7 +190,7 @@ unix % ld -shared example.o example_wrap.o -o example.so
<h3> Adding Documentation </h3>
Documentation can now be added using C/C++ comments. For example :
Documentation can be added using C/C++ comments (SWIG 1.1 only, but it is due to return at some point in SWIG 1.3). For example :
<ul><tt><pre>
/* example.i */
@ -199,12 +222,12 @@ Documentation can also be produced in ASCII and LaTeX formats.
The C/C++ comments containing documentation can span multiple lines and
can include embedded LaTeX or HTML if desired.
<h3> Running SWIG under Windows NT </h3>
<h3> Running SWIG under Microsoft Windows </h3>
SWIG also works perfectly well under Windows NT/95 systems. SWIG
SWIG also works perfectly well under all known 32 bit versions of Windows including 95/98/NT/2000/XP. SWIG
is typically invoked from the command prompt and can be used with
NMAKE. Modules are typically compiled in the form of a DLL that
can be dynamically loaded into Tcl,Python, or Perl. With a little
can be dynamically loaded into Tcl, Python, or whatever language you are using. With a little
work, SWIG can also be used as a custom build option within
MS Developer Studio.
@ -215,7 +238,7 @@ That's about everything you need to know. Here's the short checklist :
<ul>
<li> Make sure you specify a module name.
<li> Use ANSI C/C++ syntax
<li> Figure out how to compile a shared library module (may require reading a few man
<li> Figure out how to compile a shared library module / dynamic link library (may require reading a few man
pages for your compiler).
<li> Relax.
</ul>