36 SWIG and C as the target language

This chapter describes SWIG's support for creating ANSI C wrappers. This module has a special purpose and thus is different from most other modules.

NOTE: this module is still under development.

36.1 Overview

SWIG is normally used to provide access to C or C++ libraries from target languages such as scripting languages or languages running on a virtual machine. SWIG performs analysis of the input C/C++ library header files from which it generates further code. For most target languages this code consists of two layers; namely an intermediary C code layer and a set of language specific proxy classes and functions on top of the C code layer. We could also think of C as just another target language supported by SWIG. The aim then is to generate a pure ANSI C interface to the input C or C++ library and hence the C target language module.

With wrapper interfaces generated by SWIG, it is easy to use the functionality of C++ libraries inside application code written in C. This module may also be useful to generate custom APIs for a library, to suit particular needs, e.g. to supply function calls with error checking or to implement a "design by contract".

Flattening C++ language constructs into a set of C-style functions obviously comes with many limitations and inconveniences. All data and functions become global. Manipulating objects requires explicit calls to special functions. We are losing the high level abstraction and have to work around it.

36.2 Preliminaries

36.2.1 Running SWIG

Consider the following simple example. Suppose we have an interface file like:

/* File: example.i */
%module test
%{
#include "stuff.h"
%}
int fact(int n);

To build a C module (C as the target language), run SWIG using the -c option :

%swig -c example.i

The above assumes C as the input language. If the input language is C++ add the -c++ option:

$ swig -c++ -c example.i

Note that -c is the option specifying the target language and -c++ controls what the input language is.

This will generate an example_wrap.c file or, in the latter case, example_wrap.cxx file, along with example_proxy.h and example_proxy.c files. The name of the file is derived from the name of the input file. To change this, you can use the -o option common to all language modules.

The wrap file contains the wrapper functions, which perform the main functionality of SWIG: it translates the input arguments from C to C++, makes calls to the original functions and marshalls C++ output back to C data. The proxy header file contains the interface we can use in C application code. The additional .c file contains calls to the wrapper functions, allowing us to preserve names of the original functions.

36.2.2 Command line options

The following table list the additional commandline options available for the C module. They can also be seen by using:

swig -c -help 
C specific options
-noproxy do not generate proxy files (i.e. filename_proxy.h and filename_proxy.c)
-noexcept generate wrappers with no support of exception handling; see Exceptions chapter for more details

36.2.3 Compiling a dynamic module

The next step is to build a dynamically loadable module, which we can link to our application. This can be done easily, for example using the gcc compiler (Linux, MinGW, etc.):

$ swig -c example.i
$ gcc -c example_wrap.c
$ gcc -c example_proxy.c
$ gcc -shared example_wrap.o example_proxy.o -o libexample.so

Or, for C++ input:

$ swig -c++ -c example.i
$ g++ -c example_wrap.cxx
$ gcc -c example_proxy.c
$ g++ -shared example_proxy.o example_wrap.o -o libexample.so

Now the shared library module is ready to use. Note that the name of the generated module is important: is should be prefixed with lib on Unix, and have the specific extension, like .dll for Windows or .so for Unix systems.

36.2.4 Using the generated module

The simplest way to use the generated shared module is to link it to the application code during the compilation stage. We have to compile the proxy file as well. The process is usually similar to this:

$ gcc runme.c -L. -lexample -o runme

This will compile the application code (runme.c), along with the proxy code and link it against the generated shared module. Following the -L option is the path to the directory containing the shared module. The output executable is ready to use. The last thing to do is to supply to the operating system the information of location of our module. This is system dependant, for instance Unix systems look for shared modules in certain directories, like /usr/lib, and additionally we can set the environment variable LD_LIBRARY_PATH (Unix) or PATH (Windows) for other directories.

36.3 Basic C wrapping

Wrapping C functions and variables is obviously performed in a straightforward way. There is no need to perform type conversions, and all language constructs can be preserved in their original form. However, SWIG allows you to enchance the code with some additional elements, for instance using check typemap or %extend directive.

36.3.1 Functions

For each C function declared in the interface file a wrapper function is created. Basically, the wrapper function performs a call to the original function, and returns its result.

For example, for function declaration:

int gcd(int x, int y);

The output is simply:

int _wrap_gcd(int arg1, int arg2) {
  int result;
  result = gcd(arg1,arg2);
  return result;
}

Then again, this wrapper function is usually called from C using helper function declared in proxy file, preserving the original name:

int gcd(int arg1, int arg2) {
  return _wrap_gcd(arg1,arg2);
}

Now one might think, what's the use of creating such functions in C? The answer is, you can apply special rules to the generated code. Take for example constraint checking. You can write a "check" typemap in your interface file:

%typemap(check) int POSITIVE {
  if ($1 <= 0)
    fprintf(stderr, "Expected positive value in $name.\n");
}

int gcd(int POSITIVE, int POSITIVE);

And now the generated result looks like:

int _wrap_gcd(int arg1, int arg2) {
  {
    if (arg1 <= 0)
      fprintf(stderr, "Expected positive value in gcd.\n");
  }
  {
    if (arg1 <= 0)
      fprintf(stderr, "Expected positive value in gcd.\n");
  }
  int result;
  result = gcd(arg1,arg2);
  return result;
}

This time calling gcd with negative value argument will trigger an error message. This can save you time writing all the constraint checking code by hand.

36.3.2 Variables

Wrapping variables comes also without any special issues. All global variables are directly accessible from application code. There is a difference in the semantics of struct definition in C and C++. When handling C struct, SWIG simply rewrites its declaration. In C++ struct is handled as class declaration.

You can still apply some of the SWIG features when handling structs, e.g. %extend directive. Suppose, you have a C struct declaration:

typedef struct {
  int x;
  char *str;
} my_struct;

You can redefine it to have an additional fields, like:

%extend my_struct {
  double d;
};

In application code:

struct my_struct ms;
ms.x = 123;
ms.d = 123.123;

36.4 Basic C++ wrapping

The main reason of having the C module in SWIG is to be able to access C++ from C. In this chapter we will take a look at the rules of wrapping elements of the C++ language.

By default, SWIG attempts to build a natural C interface to your C/C++ code.
C++ Type SWIG C Translation
Class Example Empty structure Example
Public, mutable member variable Foo Example::foo Example_foo_get(Example *e);
Example_foo_set(Example *e, Foo *f);
Public, immutable member variable Foo Example::bar Example_foo_get(Example *e);
This section briefly covers the essential aspects of this wrapping.

36.4.1 Classes

Consider the following example. We have a C++ class, and want to use it from C code.

class Circle {
public:
  double radius;

  Circle(double r) : radius(r) { };
  double area(void);
};

What we need to do is to create an object of the class, manipulate it, and finally, destroy it. SWIG generates C functions for this purpose each time a class declaration is encountered in the interface file.

The first two generated functions are used to create and destroy instances of class Circle. Such instances are represented on the C side as pointers to special structs, called SwigObj. They are all "renamed" (via typedef) to the original class names, so that you can use the object instances on the C side using pointers like:

Circle *circle;

The generated functions make calls to class' constructors and destructors, respectively. They also do all the necessary things required by the SWIG object management system in C.

Circle * new_Circle(double r);
void delete_Circle(Circle * self);

The class Circle has a public variable called radius. SWIG generates a pair of setters and getters for each such variable:

void Circle_radius_set(Circle * self, double radius);
double Circle_radius_get(Circle * self);

For each public method, an appropriate function is generated:

double Circle_area(Circle * self);

You can see that in order to use the generated object we need to provide a pointer to the object instance (struct Circle in this case) as the first function argument. In fact, this struct is basically wrapping pointer to the "real" C++ object.

Our application code could look like this:

  Circle *c = new_Circle(1.5);
  printf("radius: %f\narea: %f\n", Circle_radius_get(c), Circle_area(c));
  delete_Circle(c);

After running this we'll get:

radius: 1.500000
area: 7.068583

Backend Developer Documentation

Typemaps

Typemap Used for
proxy Input parameters of proxy function
ctype Wrapper function declaration
wrap_call Casts functions' parameters of wrapper function calls

extern void _wrap_MyClass_delete(SwigObj *o);

void MyClass_delete(MyClass *c) {
_wrap_MyClass_delete((Swig_Obj *)c);
}
in Generated for input parameters of a function
couttype Casts return values of wrapper functions

SwigObj* _wrap_MyClass_new(void) {
void *obj = ...
return (SwigObj*)obj;
}
proxy Adds typecasts to class objects of wrapper functions calls in proxy functions
void MyClass_delete(MyClass *myClass) {
_wrap_MyClass_delete((SwigObj*)myClass);
}
couttype Adds typecasts to wrap function return values in proxy functions
MyClass_new(void) {
return (MyClass *)_wrap_MyClass_new();
}
proxycouttype Adds typecasts to wrap function return values in proxy functions
MyClass_new(void) {
return (MyClass *)_wrap_MyClass_new();
}
out Adds code to wrapper functions for the return value variables
cppouttype special case where a special cppresult variable is added to a wrapper function (TODO:the reason for its existence needs investigation).

36.5 Exception handling