See All Titles |
![]() ![]() SWIG—The Simple Wrapper Interface GeneratorSWIG (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:
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:
|
© 2002, O'Reilly & Associates, Inc. |