C and C++ low-level languages have come through ages and stood its test against writing high performance, reliable and robust code. With the introduction of the Object-Oriented paradigm, C++ was the choice language for many programmers around the world. Many of the legacy applications are designed in C++ and still run very well on newer platforms.

Having said, there are many newer scripting languages like Ruby, Python and the likes are easier to program and provide many powerful features. It’s often used for rapid application and prototyping which may be sometimes cumbersome in C/C++. Hence, the purpose and usage of these programming languages are very different from the low-level programming languages like C++. Is it possible to have the best of both of the worlds? Can we bridge between low-level and high-level language? The simple answer is Yes, it’s possible!

SWIG: As a Bridge

SWIG tool is an attempt to bridge the gap of C/C++ and high-level languages. SWIG stands for Simplified Wrapper and Interface Generator. SWIG is an interface compiler that can be used for programs written in C and C++ with scripting languages such as Ruby, Perl, Python, Ruby and the likes. We need to write an interface file which contains instructions for taking the declarations found in C/C++ header files and using them to generate the wrapper code that scripting languages need to access.

SWIG doesn’t magically convert C/C++ programs to corresponding high-level languages and its purpose is not to convert as well. We need to carefully select the APIs and interfaces which we wanted to expose to the high-level languages, this has to be defined appropriately in an interface. After this, SWIG can be used to generate appropriate wrapper and interfaces for the target high-level language we wanted these interfaces to be used.

Target Languages Supported´╗┐
Allegro LispPike
C# LuaPython
Chicken Modula 3R
D MzScheme/Racket Ruby
Go Ocaml Scilab
Guile Octave Tcl
JavaPerl5
JavascriptPHP

Advantages of SWIG

  • Using a scripting language, programs can be customized faster, do automation testing on the underlying logic and automate routine tasks associated with the program.
  • A scripting engine can be developed from the APIs exposed by C/C++ interfaces which can be used for a wide variety of uses.
  • System Programming and Integration – C/C++ are often used for system level development. This can be extended to supported high-level languages through SWIG.
  • Rapid Development and Debugging – Using SWIG, we can place our C/C++ programs into a scripting environment which is known for rapid development and powerful debugging supported by the high-level languages. Developing a UI interface in C/C++ is tedious and painful without the support of a 3rd party libraries like QT or MFC. Some of the high-level language support the rapid development of UI.

A Sample Code

rectangle.h

class Shape {
 public:
  int x,y;
  Shape() : x(0), y(0) { }
  virtual double area(void) = 0;
  virtual double perimeter(void) = 0;
};

class Rectangle: public Shape {
  public:
   int width;
   int height;
   Rectangle(int width, int height) : width(width), height(height) { }
   virtual double area(void);
   virtual double perimeter(void);
 };

rectangle.cpp

double Rectangle::area(void) {
  return width*height;
}

double Rectangle::perimeter(void) {
  return 2*width+2*height;
}

rectangle.i (SWIG Interface file)

%module rectangle
%{
#include "rectangle.h"
%}

Running the SWIG for generating wrapper for Python

> swig -python rectangle.i

SWIG Output

> rectangle_wrap.c 
> rectangle.py

rectangle_wrap.c needs to be compiled along with the source code and generate a shared library. Whereas, .py file is the Python support code that serves as a front-end to the low-level C++ module.

Usage in Python

>>> import rectangle
>>> rectangle.area(75,35)
2625
>>> rectangle.perimeter(75,35)
220

SWIG is highly flexible and supports many high level languages. Hence, this can be used in wide variety of use-cases where a support for scripting language is needed or need for high-level extension is required.