Executive Summary
"Don't keep doing what doesn't work" -- Anonymous
"Keep it simple stupid" -- Anonymous
SWIG is an interface compiler that connects programs written in C,
C++, and Objective-C with scripting languages including Perl,
Python, and
Tcl/Tk. It works by taking the declarations commonly found in C/C++ header
files and using them to generate the glue code (wrappers) that scripting
languages need to access the underlying C/C++ code.
John Ousterhout has written an excellent
paper
that describes the
benefits of scripting languages. SWIG makes it even easier to
use scripting languages by automating the process of connecting scripting
languages to C/C++ code.
SWIG is used in a number of ways:
- Building better C/C++ programs. Using SWIG, you can easily replace the main()
function of a C program with a scripting interpreter. This adds flexibility,
simplifies development, and makes it easier to build extensible applications.
When used with Tk, it also becomes possible to build simple graphical user interfaces.
- Rapid prototyping and debugging. SWIG allows C/C++ programs to be placed in
a scripting environment that can be used for testing and debugging.
For example, you might test a library with a collection of scripts or use the scripting
interpreter as an interactive debugger. Since SWIG requires no modifications to the
underlying C code, it can be used even if the final product does not rely upon scripting.
- Systems integration. Scripting languages excel at controlling and gluing
software components together. With SWIG, different C/C++ programs can be turned into
scripting language extension modules. These modules can then be combined together
to create new and interesting applications.
SWIG is sometimes compared to other scripting language extension building tools
and compilers for interface definition languages (IDL) such as CORBA IDL.
However, SWIG has a number of distinguishing features.
- ANSI C/C++ syntax. SWIG parses a form of ANSI C
syntax that has been extended with a number of special directives.
This makes it extremely easy to use since scripting interfaces
can usually be built by grabbing a header file and tweaking
it a little bit.
- Complete automation. SWIG produces a fully functional
scripting language module and is not a stub-generator. It is rarely necessary to modify the output of SWIG.
- Multiple languages. SWIG can produce extension modules
for a variety of different scripting languages. Most scripting language
extension building tools are specific to a single language.
- Designed to work with existing C/C++ code. SWIG requires little, if any, modifications
to existing code. Nor is it necessary to create special wrappers or stubs.
This allows existing applications to be easily migrated to a scripting interface.
It also allows you to maintain a clean separation of the C/C++ implementation and its
(often optional) scripting interface.
- Extensibility. SWIG gives you the full power to shoot
yourself in the foot. It can be extended and customized in any number
of interesting ways. Since C/C++ applications vary widely, SWIG is
designed to be adaptable to your application, not the other way around.
A number of papers and tutorials describing SWIG are available.
You can also view a simple tutorial to see an
example of SWIG in action.
Find out who is using SWIG and what
they said about it (from the March, 1998
user survey).