To use SWIG to generate wrapper code for your ANSI C or ANSI C++ libraries, follow these steps to install, configure, and run SWIG.
Before installing SWIG, make sure that you first install the following:
Although SWIG is capable of generating wrapper code for 13 different target languages, the instructions provided here are specific to generating Java wrapper code. SWIG is known to work on Unix (most flavors), Windows, and Macintosh, but Linux is the only platform that has been tested by NeXtMidas thus far.
$ swig -versionIf SWIG is already installed, this command will show you which version it is. If it is a version prior to 1.3.6, you will need to upgrade.
$ gunzip swig-1.3.xx.tar.gz
$ tar -xvf swig-1.3.xx.tar
$ ./configureNOTE: The SWIG installation process attempts to install the package's files in
/usr/local/bin
, /usr/local/man
, etc.
If you wish to install SWIG in an area other than the default, which
requires root access, run configure with the --prefix
option. For example, SWIG installed in the user's home directory:
$ configure --prefix=$HOME
$ make
$ make install
In the SWIG directory containing the source code, there is a directory of
Examples
for each of the supported target languages. Follow the
instructions below to generate wrapper code for the simple Java example
located in Examples/java/simple/
, (though there are many
other Java examples to try.)
$ cd Examples/java/simple/
$ swig -java example.iThis creates
example_wrap.c
and exampleJNI.java
.
$ gcc -fpic -c example.c example_wrap.c \This creates
-I$JAVAHOME/include -I$JAVAHOME/include/linux
example.o
and example_wrap.o
.
$ gcc -shared example.o example_wrap.o -o libexample.soThis creates
libexample.so
. Make sure the path to this shared
library file is defined in your LD_LIBRARY_PATH environment variable.
$ javac main.java
$ java mainYou should see the following output:
The gcd of 42 and 105 is 21If you get an UnsatisfiedLinkError, check the following:
Foo = 3.0
Foo = 3.1415926
libexample.so
loadLibrary
function does not include the .so
extension.For additional configuration and installation details consult the
README
and INSTALL
files found in the top level
SWIG directory that is created when you extract the SWIG files. Further
information about installing SWIG on different platforms or for use with
different target languages can be found at the
http://www.swig.org web site.