SWIG Tutorial

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 and Java. Specifically, let's say you have them in a file 'example.c'

Interface file

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 :

Building a Tcl module

At the UNIX prompt, type the following (shown for Linux):

unix % swig -tcl example.i
unix % gcc -fpic -c example.c example_wrap.c \
       -I/usr/local/include 
unix % gcc -shared example.o example_wrap.o -o example.so
unix % tclsh
% load ./example.so example
% puts $My_variable
3.0
% fact 5
120
% my_mod 7 3
1
% get_time
Sun Feb 11 23:01:07 1996

% 
The swig command produces a file example_wrap.c that should be compiled and linked with the rest of the program. In this case, we have built a dynamically loadable extension that can be loaded into the Tcl interpreter using the 'load' command.

If your machine does not support dynamic loading, it is also easy to build a new version of the tclsh interpreter as follows :


unix % swig -tcl -ltclsh.i example.i
unix % gcc example.c example_wrap.c -I/usr/local/include \
       -L/usr/local/lib -ltcl -lsocket -ldl -lm -o my_tclsh
unix % my_tclsh
% puts $My_variable
3.0
% fact 5
120
%
In this case, the new version of tclsh is functionally identical to the original, but has new functions added to it.

Building a Python module

Turning C code into a Python module is also easy. Simply do the following (shown for Irix):

unix % swig -python example.i
unix % gcc -c example.c example_wrap.c \
       -I/usr/local/include/python1.4 \
       -I/usr/local/lib/python1.4/config
unix % ld -shared example.o example_wrap.o -o examplemodule.so 
We can now use the Python module as follows :
>>> import example
>>> example.fact(5)
120
>>> example.my_mod(7,3)
1
>>> example.get_time()
'Sun Feb 11 23:01:07 1996'
>>>

Building a Perl module

You can also build a Perl5 module as follows (shown for Solaris):
unix % swig -perl5 example.i
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
unix % perl
use example;
print $example::My_variable,"\n";
print example::fact(5),"\n";
print example::get_time(),"\n";
<ctrl-d>
3.0
120
Sun Feb 11 23:01:07 1996
unix % 

Building a Java module

SWIG will also generate JNI code for accessing C/C++ code from Java. Here is an example building a Java module (shown for Cygwin):
$ 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
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
$

SWIG for the truly lazy

As it turns out, it is not always necessary to write a special interface file. If your C code is relatively clean, you can just run SWIG directly on the source like this :
unix % swig -tcl -module example example.c
unix % gcc -c example.c example_wrap.c -I/usr/local/include
unix % ld -shared example.o example_wrap.o -o example.so

Adding Documentation

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 : We can now run SWIG using :
This will produce a documentation file example_wrap.html .

The documentation system produces documentation using the syntax of the target language. Thus, if you used Perl5 instead of Tcl, the documentation file might look like this .

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.

Running SWIG under Microsoft Windows

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 whatever language you are using. With a little work, SWIG can also be used as a custom build option within MS Developer Studio.

That's it (well, mostly)

That's about everything you need to know. Here's the short checklist :