- 2.1 What is SWIG?
- 2.2 Why use SWIG?
- 2.4 Supported C/C++ language features
- 1.8 Backwards compatibility
- 1.12 Installation
2.1 What is SWIG?
In a nutshell, SWIG is a compiler that takes C/C++ declarations and creates the wrappers needed to access those declarations from other languages including Perl, Python, Tcl, Ruby, Guile, and Java.
c++ calling script is also called callback because c++ calls script and then script calls c++ again.
Possible applications of SWIG include:
- Building interpreted interfaces to existing C programs.
- Rapid prototyping and application development.
- Interactive debugging.
- Reengineering or refactoring of legacy software into scripting language components.
- Making a graphical user interface (using Tk for example).
- Testing of C libraries and programs (using scripts).
- Building high performance C modules for scripting languages.
- Making C programming more enjoyable (or tolerable depending on your point of view).
- Impressing your friends.
- Obtaining vast sums of research funding (although obviously not applicable to the author).
2.2 Why use SWIG?
let’s list a few problems with C/C++ programming:
- Writing a user interface is rather painful (i.e., consider programming with MFC, X11, GTK, or any number of other libraries).
- Testing is time consuming (the compile/debug cycle).
- Not easy to reconfigure or customize without recompilation.
- Modularization can be tricky.
- Security concerns (buffer overflows for instance).
2.4 Supported C/C++ language features
- Full C99 pre-processing.
- All ANSI C and C++ data types.
- Functions, variables, and constants.
- Classes.
- Single and multiple inheritance.
- Overloaded functions and methods.
- Overloaded operators.
- C++ templates (including member templates, specialization, and partial specialization).
- Namespaces.
- Variable length arguments.
- C++ smart pointers.
1.8 Backwards compatibility
If you are a previous user of SWIG, don’t expect SWIG to provide complete backwards compatibility.
If you need to work with different versions of SWIG and backwards compatibility is an issue, you can use the SWIG_VERSION preprocessor symbol which holds the version of SWIG being executed.
SWIG_VERSION is a hexadecimal integer such as 0x010311 (corresponding to SWIG-1.3.11). This can be used in an interface file to define different typemaps, take advantage of different features etc:
#if SWIG_VERSION >= 0x010311
/*Use some fancy new feature */
#endif
1.12 Installation
If you checked the code out via Git, you will have to run ./autogen.sh before ./configure.
1.12.1 Windows installation
The Windows distribution is called swigwin and includes a prebuilt SWIG executable, swig.exe and so ypu do not need to compile it.
1.12.2 Unix installation
To build and install SWIG, simply type the following:
$ ./configure
$ make
$ make install
By default SWIG installs itself in /usr/local. If you need to install SWIG in a different location or in your home directory, use the –prefix option to ./configure. For example:
$ ./configure --prefix=/home/yourname/projects
$ make
$ make install
Note: the directory given to –prefix must be an absolute pathname. Do not use the ~ shell-escape to refer to your home directory. SWIG won’t work properly if you do this.
1.12.4 Testing
if you want to test SWIG after building it, a check can be performed on Unix operating systems.
Type the following: $ make -k check
you can use parallel make to speed up the check as it does take quite some
time to run, for example: $ make -j2 -k check
Even if the check fails, there is a pretty good chance SWIG still works correctly — you will just have to mess around with one of the examples and some makefiles to get it to work.
Some tests may also fail due to missing dependency packages, eg PCRE or Boost, but this will require careful analysis of the configure output done during configuration.
1.12.5 Examples
The Examples directory also includes Visual C++ project 6 (.dsp) files for building some of the examples on Windows. Later versions of Visual Studio will convert these old style project files into a current solution file.
Ensure the SWIG executable is as supplied in the SWIG root directory in order for the examples to work. Most languages require some environment variables to be set before running Visual C++. Note that Visual C++ must be re-started to pick up any changes in environment variables.
Open up an example .dsp file, Visual C++ will create a workspace for you (.dsw file). Ensure the Release build is selected then do a Rebuild All from the Build menu. The required environment variables are displayed with their current values.
Python
PYTHON_INCLUDE : Set this to the directory that contains Python.h
PYTHON_LIB : Set this to the python library including path for linking
Example using Python 2.1.1:
PYTHON_INCLUDE: D:\python21\include
PYTHON_LIB: D:\python21\libs\python21.lib