< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039199167010047123209178152124239215162147033230210041176116136

SWIG—The Simple Wrapper Interface Generator

SWIG (Simple Wrapper and Interface Generator) is an automated tool create by David Beazley used to write interfaces between Python and existing C libraries. These interfaces can contain several single functions.

The programmer doesn't have to write any special wrapping functions to provide the glue between the Python scripting language and the C functions.

SWIG works by reading an interface file that contains function and method prototypes. It automatically does the necessary type conversion, checks the code for error, produces a C file, compiles the file, and builds it into a shared object file.

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.

SWIG is better suited as a mechanism for controlling a variety of C programs because it enables someone to combine bits and pieces of completely different software packages without waiting for someone else to write a special purpose module.

The handling of datatypes when using SWIG for prototyping and control application is very easy because whenever SWIG finds an unknown datatype, it simply assumes that it is some kind of complex datatype. Consequently, wrapping a complex C program doesn't imply too much work.

SWIG provides a convenient way of building Python interfaces to libraries.

You just need to write simple interface definitions, which SWIG uses to generate the C program that conforms to the Python/C extension guidelines.

SWIG makes it even easier to use scripting languages by automating the process of connecting scripting languages to C/C++ code.

Many reasons you should try SWIG are as follows:

You can easily replace the main() function of a C program with Python's interpreter.

C/C++ code is easily tested because you can call C functions and libraries directly from your scripting environment.

Debugging your C code also becomes easier once you use Python's interpreter. Remember that you don't need to change your C code in order to use SWIG.

SWIG can integrate different C/C++ programs together by turning them into extension modules. After the extensions are created, Python can combine and use them to generate new applications.

SWIG understands and parses ANSI C/C++ syntax.

The output of SWIG is a fully functional scripting language module.

As SWIG is designed to work with existing C/C++ code, it will be rarely necessary to change your existing programs.

Your C/C++ code remains separate from your Python code.

SWIG output can be freely extended and customized.

Now, the most interesting thing is that you don't need to master all the details about the Python/C API in order to use the basics of SWIG to create your Python extension modules. SWIG automates the process of generating a Python extension based on the header of the functions that you want to export.

Take a look at the following example and see how simple it is to generate a wrapper file. We will first create an input file, and call it helloworld.i.

					
// file: helloworld.i

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

char *say();

				

Now, we will use SWIG to generate the wrapper file. We need to pass an argument to SWIG informing that the wrapper must be created for the Python language. That's because SWIG works with many different languages.

					
% swig -python helloworld.i
Generating wrappers for Python…
%

				

As you can see, a wrapper file called helloworld_wrap.c was created for you.

More information about SWIG can be found at the following Web pages:

SWIG official Web site:

http://www.swig.org

SWIG Users Guide—Chapter 9, "SWIG and Python" :

http://www.swig.org/Doc1.1/PDF/Python.pdf

"Using SWIG to Control, Prototype, and Debug C Programs with Python":

http://www.swig.org/papers/Py96/python96.html

"Feeding a Large-scale Physics Application to Python":

http://www.swig.org/papers/Py97/beazley.html

"Interfacing C/C++ and Python with SWIG":

http://www.swig.org/papers/PyTutorial97/PyTutorial97.pdf

"The Benefits of Scripting Languages," by John Ousterhout:

http://www.scriptics.com/people/john.ousterhout/scripting.html


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

applications
     non-Python
            embedding Python objects in;Simplified Wrapper and Interface Generator (SWIG) 2nd 3rd 4th
Beazley, David
C programming language
      extending and embedding Python 2nd 3rd 4th
C++ programming language
      extending and embedding Python 2nd 3rd 4th
code
     glueÓ
            Ò
creating
     Python extension modules
            Simplified Wrapper and Interface Generator (SWIG) 2nd 3rd 4th
embedding
     Python objects
            Simplified Wrapper and Interface Generator (SWIG) 2nd 3rd 4th
files
     wrapper
            generating 2nd
generating
      wrapper files 2nd
glue codeÓ
      Ò
objects
     Python
            embedding in non-Python applications;Simplified Wrapper and Interface Generator (SWIG) 2nd 3rd 4th
programming languages
     C
            extending and embedding Python 2nd 3rd 4th
     C++
            extending and embedding Python 2nd 3rd 4th
programs
     non-Python
            embedding Python objects in;Simplified Wrapper and Interface Generator (SWIG) 2nd 3rd 4th
Simplified Wrapper and Interface Generator (SWIG) 2nd 3rd 4th
software
     non-Python
            embedding Python objects in;Simplified Wrapper and Interface Generator (SWIG) 2nd 3rd 4th
tools
      Simplified Wrapper and Interface Generator (SWIG) 2nd 3rd 4th
utilities
      Simplified Wrapper and Interface Generator (SWIG) 2nd 3rd 4th
wrapper files
      generating 2nd

© 2002, O'Reilly & Associates, Inc.