SWIG - Way to link High level Programming languages and C/C++ modules
User Rating: / 1
PoorBest 
Written by khushboo diwakar   







swig_img_intro.png

SWIG(Simplified Wrapper and Interface Generator) is a software development tool , an interface compiler that connects programs written in C and C++ with a variety of scripting languages such as Perl, Python, Ruby, and Tcl and also non-scripting languages such as C#, Common Lisp (CLISP, Allegro CL, CFFI, UFFI), Java, Lua, Modula-3, OCAML and R. . "Scripts" are often treated as distinct from programs  which execute independently from any other application.

Many times we need to link C/C++ modules or small functions with a different language for our projects There are two main purposes of embedding a scripting language into an existing C/C++ program:

  1. The program can then be customized much more quickly,by using scripting languages rather than C/C++. The scripting engine may even be exposed to the end user, so that they can automate common tasks by writing scripts.
  2. Even if the final product is not to contain the scripting engine, it may nevertheless be quite useful to write testing scripts.

Its working is as follows

  1.  Takes the declarations found in C/C++ header files 
  2.  Generates the wrapper code needed to access the underlying C/C++ code.

In addition, SWIG provides a variety of customization features that let us tailor the wrapping process to suit our application. SWIG may be freely used, distributed, and modified for commercial and non-commercial use.

 

SWIG is used in a number of ways:

 Building more powerful C/C++ programs. Using SWIG, we can replace the main() function of a C program with a scripting interpreter from which we can control the application. This adds quite a lot of flexibility and makes the program "programmable." That is, the scripting interface allows users and developers to easily modifiy the behavior of the program without having to modify low-level C/C++ code. The benefits of this are numerous. In fact think of all of the large software packages that you use every day---nearly all of them include special a macro language, configuration language, or even a scripting engine that allows users to make customizations.

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/C++ code, it can be used even if the final product does not rely upon scripting.

Systems integration. Scripting languages work fairly well for controlling and gluing loosely-coupled 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.

Construction of scripting language extension modules. SWIG can be used to turn common C/C++ libraries into components for use in popular scripting languages. Of course, you will still want to make sure that no-one else has already created a module before doing this.


 

Well I got a very good example explaining the use of SWIG on this site http://www.swig.org/tutorial.html. Nothing can explain better than this.

 

Let's say we have modules in a file 'example.c'

/* File : example.c */
#include <time.h>
double My_variable = 3.0;
/*function to find the factorial of a number*/
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
/*function to find the remainder after dividing two numbers*/
int my_mod(int x, int y) {
return (x%y);
}

char *get_time()
{
time_t ltime;
time(&ltime);
return ctime(&ltime);
}


Interface file
Now, in order to add these files to our favorite language, we need to write an "interface file" which is the input to SWIG. An interface file for these C functions might look like this :

/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
Building a Java module
SWIG will also generate JNI code for accessing C/C++ code from Java shown here for cygwin. Here is an example building a Java module

$ swig -java example.i
$ gcc -c example.c example_wrap.c -I/c/jdk1.3.1/include -I/c/jdk1.3.1/include/win32
$ gcc -shared example.o example_wrap.o -mno-cygwin -Wl,--add-stdcall-alias -o example.dll
$ cat main.java
public class main {
public static void main(String argv[]) {
System.loadLibrary("example");
System.out.println(example.getMy_variable());
System.out.println(example.fact(5));
System.out.println(example.get_time());
}
}
$ javac main.java
$ java main
3.0
120
Mon Mar 4 18:20:31 2002
$

This is an example facilitating the linking of a java module with C/C++. For other high level languages refer http://www.swig.org/tutorial.html.

SWIG 1.3.33 is the latest development release. Windows users should download swigwin-1.3.33 which includes a prebuilt executable.

The release is available for download on Sourceforge at http://prdownloads.sourceforge.net/swig/swig-1.3.33.tar.gz

A Windows version should also be available at http://prdownloads.sourceforge.net/swig/swigwin-1.3.33.zip







 

Quote this article on your site

  Be first to comment this article

Only registered users can write comments.
Please login or register.


Powered by AkoComment Tweaked Special Edition v.1.4.6
AkoComment © Copyright 2004 by Arthur Konze - www.mamboportal.com. All right reserved

 
< Prev




Privacy Notice | Advertising Info | Feedback | Contact Us | Partners

The information and views presented above are by the author. Mabaloo.com does not take any gurantee of accuracy.
The views expressed above are that of the author and in no way related to mabaloo.com
Highlite It
© 2007 @ mabaloo.com.