Simplified Wrapper and Interface Generator

March 1st, 2000 by Wael Hassan in

An introduction to the uses and advantages of SWIG.
Your rating: None

SWIG (simplified wrapper and interface generator) is a software development tool that connects programs written in C, C++ and Objective-C with a variety of high-level programming languages. It is often used with common scripting languages such as Perl, Python and Tcl/Tk. In addition, it has been extended to include languages such as Java, Eiffel and Guile.

SWIG is used to create high-level interpreted programming environments for systems integration, and as a tool for building user interfaces and testing. It is distributed as open source and can be downloaded from http://www.swig.org/.

In the following sections, I will discuss some of SWIG's features and my personal experience with it as a testing engineer at Zero Knowledge Systems (http://www.zeroknowledge.com/) for the Freedom Software.

Environment and Mission

At work, I have to test e-mail, web, IRC, FTP, proxy servers and TELNET clients. My machine is a Pentium powered by a 266MHz processor running Red Hat Linux with 128MB of RAM. My responsibilities include testing the source code currently being developed—hundreds of thousands of lines of code in a mission-critical system, with no room for errors. Given that “the strength of a security system is the strength of its weakest link”, there was no place for flaws. The code cannot be tested manually, because of the distributed architecture of client and servers. Thus, there was an urgent need for an efficient tool that would automate testing procedures. This tool had to be platform-independent and compliant with both C and C++.

Then There was Light!

Shopping around, I learned about SWIG from its web page. The source can be easily compiled for Linux; it is about 2MB in size. SWIG is multi-platform, i.e., there is no need to duplicate test procedures for Linux and Windows; it supports C and C++; and it can be integrated into MSVC++ (Microsoft Visual C++). SWIG proved to be the perfect tool.

Let Us SWIG Together

SWIG accepts as input an ANSI C-like interface file that describes the functions and objects constituting the program to be tested. The interface file can also include SWIG directives and documentation. SWIG wraps the functions in another C program. When both of these programs (the source code and the wrapped source code) get compiled, SWIG creates a library file that can be called from the Tcl shell.

Step By Step
  • The Program: start by writing your C program to be tested. One thing to note is you have to modify the name of the “main” function. Listing 1 is an example of a C program.

Listing 1

  • Interface file: in order to allow SWIG control over this program, we have to write an “interface file”. An interface file for our C functions might look like the one in Listing 2.

Listing 2
  • Build a Tcl module: at the prompt, type the following:

swig -tcl8 my_interface.i
This command will create a Tcl 8.0-compliant library.
  • Compile wrappers for Tcl using the commands

        gcc -fpic -c example.c example_wrap.c\
           -I/usr/local/include
        gcc -shared example.o example_wrap.o\
           -o example.so
  • Call the Tcl shell by typing tclsh.

  • Load the example.so library with the command

  • load ./example.so example

Now, feel free to call the functions implemented in the C program:
get_time
Sun Feb 11 23:01:07 1996

Taking Care of Business

SWIG helped me a lot, due to the flexibility of function calling it provides. The company had a secure mail system to be tested. In this system, all e-mail messages go through several servers before they reach their final destination, and they are encrypted each time they pass through a new routing server.

My approach toward testing this environment was to write an e-mail generator program in C which I called GenerateMail. GenerateMail accepts several options such as the number of To, CC and Bcc copies, the number of file attachments, etc. It produces a file ready to be piped to Sendmail.

A typical GenerateMail run would be something like:

tclsh generate_mail -Attachments 3 -CC 2\
   -output file msg.txt
tclsh send_mail msg.txt

The first line creates an e-mail message file. The message has three target addresses and two carbon copies. Three binary files were attached as MIME attachments. By default, GenerateMail uses bitmaps that are in its current directory.

The second line calls Sendmail with the appropriate options to accept that mail message and send it on to the wire. Doing that, it was easy to generate a large number of mail messages. In addition, comparing the source and destination message checksums was very easy with the help of SWIG.

SWIG Advantages

SWIG's advantages are evident in a number of ways. It adds flexibility to testing procedures, it can be used for prototyping and helps in system integration. Very few modifications of the code are needed. Moreover, SWIG directives can be easily extended.

On the other hand, the wrapper has some limitations when it comes to C and C++ programs, because SWIG's author did not mean it to be a fully blown parser.

The following are some examples of unacceptable input.

  • Functions with variable-length parameters will not work.

  • Function pointers and array declarations are problematic; they could be hidden from the interface file by using typdef.

  • Most features of C++ such as templates and operator overloading are not supported.

Nevertheless, SWIG is still a powerful tool, especially since it can be integrated with Microsoft Visual C++.

The End

SWIG is a powerful tool that runs on multiple platforms and supports multiple languages. It served my goal of testing the system within the specified time constraints. It is a neat tool that still has much room for expansion. If you are in the business of testing, prototyping SWIG is the way to go.

Wael Hassan is a graduate student in computer science at Concordia University. He is studying for his master's degree and has interests in Linux, cryptography, CORBA and e-commerce. Wael can be reached by e-mail at wael@acm.org.

__________________________


Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Sorry, offer available in the US only. International orders, click here.

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Featured Videos

The November 13, 2008 edition of Linux Journal Live! Shawn Powers and special guest, Linux Journal Author Daniel Bartholomew, talk e-book readers and Daniel's Kindle, DRM, and other goodness.

From the Magazine

December 2008, #176

The Oxford English Dictionary says the word "gadget" is a placeholder name for a technical item whose precise name one can't remember. Like that book-reader thingy from Amazon...what's it called? Spindle, Gindle...Kindle, that's it. Check it out in this month's gadget issue.

Other gadgets covered include the Nokia tablets, the BlackBerry, the Neo FreeRunner, the Dash Express, the Roku Netflix Player, the Kangaroo TV, The TomTom GO 930 and the MooBella Ice Cream System. On the larger hardware front, read the reviews of the Acer Aspire One and the YDL PowerStation. On the software front, check out the articles and columns on memcached, Samba security, Mutt, desktop gadgets, bash and Puppet. To wrap it all up, read Doc's thoughts on Google and the browser platform.

Read this issue

Sign up for our Email Newsletter