4 Multiple files and the SWIG library

For increased modularity and convenience, it is often useful to break an interface specification up into multiple files or modules. This chapter describes SWIG's support for library files.

The %include directive

The %include directive inserts code from another file into the current interface file. It is primarily used to build a package from a collection of smaller modules. For example :

// File : interface.i
%module package
%include "equations.i"
%include "graphics.i"
%include "fileio.i"
%include "data.i"
%include "network.c"
%include "../Include/user.h"

In this case, SWIG creates a single wrapper file for a module package that contains all of the included declarations. Repeated %module directives in other files are simply ignored.

The %include directive can process SWIG interface files, C header files, and C source files (provided they are sufficiently clean). When processing a C source file, SWIG automatically declares all functions it finds as "extern". Thus, use of a header file may not be required in this case. Running SWIG on C++ source files is not recommended due to parser limitations

The %import directive

The %import directive is used to gather declarations from modules that are wrapped in a separate module or from files that you don't want to wrap into the current interface. The primary purpose is to collect type information and information about base classes.

For example, if you wanted to extract some common typedef declarations from a header file, you might write this:

%module foo
%import "types.h"
...
Similarly, if you are working with multiple SWIG modules, you might write the following to pick up C++ base classes. For example:
%module foo
// Grab the base class
%import "base.i"

// Define a derived class
class Foo : public Base {
...
};
If a file included with %import contains a %module directive, it is sometimes used by the target language module to coordinate the operation of more than one SWIG dynamically loaded module. This is an advanced topic that is described in the chapter on the SWIG runtime libraries.

Including files on the command line

Like the C or C++ compiler, SWIG can also include library files on the command line using the -l option as shown

# Include a library file at compile time
% swig -tcl -lwish.i interface.i

This is particularly useful for debugging and building extensions to different kinds of languages. When libraries are specified in this manner, they are included after all of the declarations in interface.i have been wrapped. Thus, this does not work if you are trying to include common declarations, typemaps, and other files.

The SWIG library

SWIG comes with a library of functions that can be used to build up more complex interfaces. As you build up a collection of modules, you may also find yourself with a large number of interface files. Although the %include directive can be used to insert files, it also searches the files installed in the SWIG library (think of this as the SWIG equivalent of the C library). When you use %include or %import, SWIG searches for files in the following order:

Within each directory, you can also create subdirectories for each target language. If found, SWIG will search these directories first, allowing the creation of language-specific implementations of a particular library file.

You can override the location of the SWIG library by setting the SWIG_LIB environment variable.

Library example

The SWIG library is really a repository of useful modules that can be used to build better interfaces. To use a library file, simply use the %include directive with the name of a library file. For example :

%module example

%include pointer.i 				// Grab the SWIG pointer library

// a+b --> c
extern double add(double a, double b, double *c);

In this example, we are including the SWIG pointer library that adds functions for manipulating C pointers. These added functions become part of your module that can be used as needed. For example, we can write a Tcl script like this that involves both the add() function and two functions from the pointer.i library :

set c [ptrcreate double 0]    ;# Create a double * for holding the result
add 4 3.5 $c                  ;# Call our C function
puts [ptrvalue $c]            ;# Print out the result

Creating Library Files

It is easy to create your own library files. To illustrate the process, we consider two different library files--one to build a new tclsh program, and one to add a few memory management functions.

tclsh.i

To build a new tclsh application, you need to supply a Tcl_AppInit() function. This can be done using the following SWIG interface file (simplified somewhat for clarity) :

// File : tclsh.i
%{
#if TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION >= 4
int main(int argc, char **argv) {
  Tcl_Main(argc, argv, Tcl_AppInit);
  return(0);
}
#else
extern int main();
#endif
int Tcl_AppInit(Tcl_Interp *interp){
  int SWIG_init(Tcl_Interp *); 

  if (Tcl_Init(interp) == TCL_ERROR) 
    return TCL_ERROR;

  /* Now initialize our functions */

  if (SWIG_init(interp) == TCL_ERROR)
    return TCL_ERROR;

  return TCL_OK;
}
%}
In this case, the entire file consists of a single code block. This code will be inserted directly into the resulting wrapper file, providing us with the needed Tcl_AppInit() function.

malloc.i

Now suppose we wanted to write a file malloc.i that added a few memory management functions. We could do the following :

// File : malloc.i
%{
#include <malloc.h>
%}

%typedef unsigned int size_t
void *malloc(size_t nbytes);
void *realloc(void *ptr, size_t nbytes);
void free(void *);

In this case, we have a general purpose library that could be used whenever we needed access to the malloc() functions. Since this interface file is language independent, we can use it anywhere.

Placing the files in the library

Although both of our examples are SWIG interface files, they are quite different in functionality since tclsh.i would only work with Tcl while malloc.i would work with any of the target languages. Thus, you should put these files into the SWIG library as follows :


./swig_lib/malloc.i
./swig_lib/tcl/tclsh.i

When used in other interface files, this allows us to use malloc.i with any target language while tclsh.i will only be accessible if creating for wrappers for Tcl (ie. when creating a Perl5 module, SWIG will not look in the tcl subdirectory.

It should be noted that language specific libraries can mask general libraries. For example, if you wanted to make a Perl specific modification to malloc.i, you could make a special version and call it ./swig_lib/perl5/malloc.i. When using Perl, you'd get this version, while all other target languages would use the general purpose version.

Working with library files

There are a variety of additional methods for working with files in the SWIG library described next.

Wrapping a library file

If you would like to wrap a file in the SWIG library, simply give SWIG the name of the appropriate library file on the command line. For example :

unix > swig -python pointer.i

If the file pointer.i is not in the current directory, SWIG will look it up in the library, generate wrapper code, and place the output in the current directory. This technique can be used to quickly make a module out of a library file regardless of where you are working.

Checking out library files

At times, it is useful to check a file out of the library and copy it into the working directory. This allows you to modify the file or to simply retrieve useful files. To check a file out of the library, run SWIG as follows :

unix > swig -co -python array.i
array.i checked out from the SWIG library
unix >

The library file will be placed in the current directory unless a file with the same name already exists (in which case nothing is done).

The SWIG library is not restricted to interface files. Suppose you had a Perl script that you liked to use a lot. You could place this in the SWIG library. Now whenever you wanted to use it, you could retrieve it by issuing :

unix > swig -perl5 -co myscript.pl
myscript.pl checked out from the SWIG library

Similarly, the library also contains Makefiles to build various extensions. For example, if you need a quick Makefile for building Tcl extension, type the following:

unix> swig -tcl -co Makefile
Makefile checked out from the SWIG library

During installation, SWIG creates a collection of preconfigured Makefiles for various scripting languages. If you need to make a new module, just check out one of these Makefiles, make a few changes, and you should be ready to compile and extension for your system.


SWIG 1.3 - Last Modified : August 18, 2001