Simplified Wrapper And Interface Generator

A kit to generate bindings from the objects in a range of languages to CeeLanguage and CeePlusPlus. It comes with ready-made bindings for Tcl/C, Tcl/C++, Perl/C, Perl/C++, Python/C. Python/C++, and many other combinations. It takes a file which describes the interface to C or C++ and generates wrapper code.

This is a way to do AlternateHardAndSoftLayers.

Excerpting the web site (http://www.swig.org/) (edited to provide links):

SWIG is a software development tool that connects programs written in CeeLanguage and CeePlusPlus with a variety of high-level programming languages. SWIG is used with different types of target languages including common scripting languages such as PerlLanguage, PhpLanguage, PythonLanguage, ToolCommandLanguage and RubyLanguage. The list of supported languages also includes non-scripting languages such as CsharpLanguage, CommonLisp (CLISP, Allegro CL, CFFI, UFFI), GoLanguage, JavaLanguage, LuaLanguage, ModulaThree, OcamlLanguage, Octave and R (ArrLanguage). Also several interpreted and compiled Scheme implementations (GuileScheme, MzScheme, ChickenScheme) are supported. SWIG is most commonly used to create high-level interpreted or compiled programming environments, user interfaces, and as a tool for testing and prototyping C/C++ software. SWIG is typically used to parse C/C++ interfaces and generate the 'glue code' required for the above target languages to call into the C/C++ code. SWIG can also export its parse tree in the form of XML and Lisp s-expressions. SWIG is free software and the code that SWIG generates is compatible with both commercial and non-commercial projects.


New Version June 2014 Version 3.0.2 has been released


For an up to date list of the target languages supported see http://www.swig.org/compat.html. The list does not include Smalltalk.

Note: The support for EiffelLanguage to be found at http://efsa.sourceforge.net/archive/cozzi/swigeiffel.htm is an addon module which was designed to run with old versions of SmallEiffel and SWIG.


To use it a program is written in an interface language which defines the interface. In the case of C++, classes can be interfaced and extended to provide functions not in the C++ code. The interface is processed by SWIG to create a C or C++ code file which is compiled to provide the extension functions for the target language. The compiled code is then linked to form a dynamic library which can be loaded by the scripting language when needed.


I have used SWIG extensively to interface CeePlusPlus code to ToolCommandLanguage and RubyLanguage. SWIG will now directly interface C++ template objects. Something which takes this one step further is RubyEmbed. -- JohnFletcher


SWIG can also be used to test/debug/explore C or C++ code using one of the supported HLL. This is described in details at http://ic.net/~cflynt/realprog/realwrld/beazley/tclchap.htm or http://www.python.org/workshops/1996-06/papers/d.beazley-swig-extending.html -- MaxIschenko

There are those who competed C++->Swig->Python against ->BPL-> and found that BoostPythonLibrary (see BoostLibraries) won hands down. For only the two languages; not "any language to any other language".

There is also PythonCxxModule for C++ and PythonLanguage.


I just used this to interface Java to two sizeable C libraries (SDL and SDL_image) and was amazed by how easy it was. From beginning to download SWIG to running a Java program that drew graphics on screen using SDL took about four hours (and I used a 56k modem to download SWIG!). I think the fact that SDL is a very well engineered C library with a simple function-based API helped a lot. --NatPryce


I've used SWIG fairly extensively in the past - and for importing C libraries into your favourite scripting language (in my case Python) it is hard to beat. HighlyRecommended?. Sadly, it doesn't work as well for "real" C++ libraries, but then again that is a much much harder problem to solve.There are some BoostLibraries ( http://www.boost.org/ ) that promise to make that easier, although I haven't had occasion to try them yet. -- BurkhardKloss


CategoryProgrammingLanguage CategoryCpp CategorySoftwareTool


EditText of this page (last edited July 24, 2014) or FindPage with title or text search