SWIG and C as the target language

# 38 SWIG and C as the target language

- Overview
  - Known C++ Shortcomings in Generated C API
- Preliminaries
  - Running SWIG
  - Command line options
  - Compiling a dynamic module
  - Using the generated module
- Basic C wrapping
  - Functions
  - Variables
- Basic C++ wrapping
  - Enums
  - Classes
- Backend Developer Documentation
- Typemaps
  - C Typemaps, a Code Generation Walkthrough
    - The Interface
    - The Wrapper
    - The Proxy
- Exception handling
- C++ Wrappers
  - Additional customization possibilities
  - Exception handling

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

NOTE: this module is still under development.

## 38.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 ISO 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, but this module is actually also capable of generating C++ wrappers defined completely inline using the C functions, thus wrapping the original C++ library API in another, similar C++ API. Contrary to the natural initial reaction, this is far from being completely pointless, as wrapping C++ API in this way avoids all problems due to C++ ABI issues, e.g. it is now possible to use the original C++ API using a different C++ compiler, or a different version of the same compiler, or even the same compiler, but with different compilation options affecting the ABI. The C++ wrapper API is not identical to the original one, but strives to be as close to it as possible.

### 38.1.1 Known C++ Shortcomings in Generated C API

- Enums with a context like class or namespace are broken
- Global variables are not supported
- Qualifiers are stripped
- Vararg functions are not supported.

## 38.2 Preliminaries

### 38.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_wrap.h` (the same extension is used in both C and C++ cases for the last one). The names of the files are derived from the name of the input file by default, but can be changed using the `-o` and `-oh` options common to all language modules.

The `xxx_wrap.c` file contains the wrapper functions, which perform the main functionality of SWIG: each of the wrappers translates the input arguments from C to C++, makes calls to the original functions and marshals C++ output back to C data. The `xxx_wrap.h` header file contains the declarations of these functions as well as global variables.

### 38.2.2 Command line options

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

    $ swig -c -help

C specific options
-namespace <nspace> | Generate wrappers with the prefix based on the provided namespace, e.g. if the option value is `outer::inner`, the prefix `outer_inner_` will be used. Notice that this is different from using SWIG `nspace` feature, as it applies the prefix to all the symbols, regardless of the namespace they were actually declared in. Notably, this provides a way to export instantiations of templates defined in the `std` namespace, such as `std::vector`, using a custom prefix rather than `std_`.
-nocxx | Don't generate C++ wrappers, even when the `-c++` option is used. See C++ Wrappers section for more details.
-noexcept | generate wrappers with no support for exception handling; see Exceptions chapter for more details

### 38.2.3 Compiling a dynamic module

The next step is to build a dynamically loadable module, which we can link to our application. For example, to do this using the `gcc` compiler (Linux, MinGW, etc.):

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

Or, for C++ input:

    $ swig -c++ -c example.i
    $ g++ -fPIC -c example_wrap.cxx
    $ g++ -shared 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.

### 38.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. The process is usually similar to this:

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

This will compile the application code (`runme.c`) 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.

## 38.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 enhance the code with some additional elements, for instance using a `check` typemap or `%extend` directive.

It is also possible to output arbitrary additional code into the generated header by using the `%insert` directive with `cheader` section, e.g.

    %insert("cheader") %{
    #include "another.h"
    %}

### 38.3.1 Functions

For each C function declared in the interface file a wrapper function with a prefix, required to make its name different from the original one, is created. The prefix for the global functions is `module_`, i.e. the name of the SWIG module followed by underscore, by default. If `-namespace` option is used, the prefix corresponding to the given fixed namespace is used instead. If `nspace` feature is used, the prefix corresponding to the namespace in which the function is defined is used -- note that, unlike with `-namespace` option, this prefix can be different for different functions. The wrapper function performs a call to the original function, and returns its result.

For example, for function declaration in the module `mymath`:

    int gcd(int x, int y);

The output is simply:

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

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 for parameter $1 in $name.\n");
    }

    int gcd(int POSITIVE, int POSITIVE);

And now the generated result looks like:

    int _wrap_gcd(int arg1, int arg2) {
      int result;
      {
        if (arg1 <= 0)
          fprintf(stderr, "Expected positive value for parameter arg1 in gcd.\n");
      }
      {
        if (arg2 <= 0)
          fprintf(stderr, "Expected positive value for parameter arg2 in gcd.\n");
      }
      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.

### 38.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;

## 38.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.

### 38.4.1 Enums

C enums and unscoped C++ enums are simply copied to the generated code and both the enum itself and its elements keep the same name as in the original code unless `-namespace` option is used or `nspace` feature is enabled, in which case the prefix corresponding to the specified namespace is used.

For scoped C++11 enums, the enum name itself is used as an additional prefix.

### 38.4.2 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 * Circle_new(double r);
    void Circle_delete(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 = Circle_new(1.5);
      printf("radius: %f\narea: %f\n", Circle_radius_get(c), Circle_area(c));
      Circle_delete(c);

After running this we'll get:

    radius: 1.500000
    area: 7.068583

## 38.5 Backend Developer Documentation

## 38.6 Typemaps

Typemap | Used for
--- | ---
`ctype` | Provides types used for the C API and Typecasts wrapper functions return values in proxy functions
    MyClass *MyClass_new(void) {
      return (MyClass *)MyClass_new();
    }
`in` | Mapping of wrapper functions parameters to local C++ variables
    SwigObj* MyClass_do(SwigObj *carg1) {
      SomeCPPClass *arg1 = 0;
      if (carg1)
        arg1 = (SomeCPPClass*)carg1->obj
      else
        arg1 = 0;
    }
`out` | Assigns wrapped function's return value to a dedicated return variable, packaging it into SwigObj if necessary
`cppouttype` | Type of the result variable used for the return value if the wrapped function is a C++ function
`cxxintype` | Defines the type for the parameters of C++ wrapper functions corresponding to this type. By default is the same as `ctype`, but may sometimes be different to make the functions more convenient to use. For example, `ctype` for `std::string` is `const char*`, but `cxxintype` typemap for it is `std::string const&`, i.e. even though the C++ string passed as a raw pointer via C API, the C++ wrapper still accepts a C++ string. If this typemap is defined, `cxxin` should normally be defined as well. If it is not defined, `ctype` is used.
`cxxouttype` | Similar to `cxxintype`, but is used for the function return values and together with `cxxout` typemap. Also defaults to `ctype` if not defined.
`cxxin` | Defines how to transform `cxxintype` value to `ctype`
`cxxout` | Defines how to transform `ctype` value returned by a function to `cxxouttype`
`cxxcode` | May contain arbitrary code that will be injected in the declaration of the C++ wrapper class corresponding to the given type. Ignored for non-class types. The special variable `$cxxclassname` is replaced with the name of the class inside this typemap expansion and `$cclassptrname` is replaced with the name of the pointer type used to represent the class in C wrapper functions.

### 38.6.1 C Typemaps, a Code Generation Walkthrough

To get a better idea of which typemap is used for which generated code, have a look at the following 'walk through'.

Let's assume we have the following C++ interface file, we'd like to generate code for:

#### 38.6.1.1 The Interface

    %module example

    %inline
    %{
      class SomeClass{};
      template <typename T> class SomeTemplateClass{};
      SomeClass someFunction(SomeTemplateClass<int> &someParameter, int simpleInt);
    %}

    %template (SomeIntTemplateClass) SomeTemplateClass<int>;

What we would like to generate as a C interface of this function would be something like this:

    // wrapper header file
    typedef struct SwigObj_SomeClass SomeClass;

    SomeClass * SomeClass_new();

    void SomeClass_delete(SomeClass * carg1);

    SomeClass* someFunction(SomeIntTemplateClass* carg1, int carg2);

    typedef struct SwigObj_SomeIntTemplateClass SomeIntTemplateClass;

    SomeIntTemplateClass * SomeIntTemplateClass_new();

    void SomeIntTemplateClass_delete(SomeIntTemplateClass * carg1);

#### 38.6.1.2 The Wrapper

We'll examine the generation of the wrapper function first.

    SWIGEXPORTC SwigObj * module_someFunction(SwigObj * carg1, int carg2) {
      SomeClass * cppresult;
      SomeTemplateClass< int > *arg1 = 0 ;
      int arg2 ;
      SwigObj * result;

      {
        if (carg1)
        arg1 = (SomeTemplateClass< int > *) carg1->obj;
        else
        arg1 = (SomeTemplateClass< int > *) 0;
      }
      arg2 = (int) carg2;
      {
        const SomeClass &_result_ref =  someFunction(*arg1,arg2);cppresult = (SomeClass*) &_result_ref;
      }
      {
        result = SWIG_create_object(cppresult, SWIG_STR(SomeClass));
      }
      return result;
    }

It might be helpful to think of the way function calls are generated as a composition of building blocks. A typical wrapper will be composited with these [optional] blocks:

1. Prototype
2. C return value variable
3. Local variables equal to the called C++ function's parameters
4. [C++ return value variable]
5. Assignment (extraction) of wrapper parameters to local parameter copies
6. [Contract (e.g. constraints) checking]
7. C++ function call
8. [Exception handling]
9. [Assignment to C++ return value]
10. Assignment to C return value

Let's go through it step by step and start with the wrapper prototype

    ctype                        ctype            ctype
    ---------                    ---------        ---
    SwigObj * module_someFunction(SwigObj * carg1, int carg2);

As first unit of the wrapper code, a variable to hold the return value of the function is emitted to the wrapper's body

    ctype
    ---------
    SwigObj * result;

Now for each of the C++ function's arguments, a local variable with the very same type is emitted to the wrapper's body.

    SomeTemplateClass< int > *arg1 = 0 ;
    int arg2 ;

If it's a C++ function that is wrapped (in this case it is), another variable is emitted for the 'original' return value of the C++ function. At this point, we simply 'inject' behavior if it's a C++ function that is wrapped (in this case it obviously is).

    cppouttype
    -----------
    SomeClass * cppresult;

Next, the values of the input parameters are assigned to the local variables using the 'in' typemap.

    {
      if (carg1)
      arg1 = (SomeTemplateClass< int > *) carg1->obj;
      else
      arg1 = (SomeTemplateClass< int > *) 0;
    }
    arg2 = (int) carg2;

A reasonable question would be: "Why aren't the parameters assigned in the declaration of their local counterparts?" As seen above, for complex types pointers have to be verified before extracting and casting the actual data pointer from the provided SwigObj pointer. This could easily become messy if it was done in the same line with the local variable declaration.

At this point we are ready to call the C++ function with our parameters.

    {
      const SomeClass &_result_ref =  someFunction(*arg1,arg2);cppresult = (SomeClass*) &_result_ref;
    }

Subsequently, the return value is assigned to the dedicated return value variable using the 'out' typemap

    {
      result = SWIG_create_object(cppresult, SWIG_STR(SomeClass));
    }

Finally, the return value variable is returned.

    return result;

Note that typemaps may use `$null` special variable which will be replaced with either `0` or nothing, depending on whether the function has a non-void return value or not.

#### 38.6.1.3 The Proxy

Compared to the wrapper code generation, the header code is very simple. Basically it contains just the declarations corresponding to the definitions above.

    // wrapper header file
    typedef struct SwigObj_SomeClass SomeClass;

    SomeClass * SomeClass_new();

    void SomeClass_delete(SomeClass * carg1);

    SomeClass* someFunction(SomeIntTemplateClass* carg1, int carg2);

    typedef struct SwigObj_SomeIntTemplateClass SomeIntTemplateClass;

    SomeIntTemplateClass * SomeIntTemplateClass_new();

    void SomeIntTemplateClass_delete(SomeIntTemplateClass * carg1);

## 38.7 Exception handling

Any call to a C++ function may throw an exception, which cannot be caught by C code. Instead, the special `SWIG_CException_get_pending()` function must be called to check for this. If it returns a non-null pointer, `SWIG_CException_msg_get()` can be called to retrieve the error message associated with the exception. Finally, `SWIG_CException_reset_pending()` must be called to free the exception object and reset the current pending exception. Note that exception handling is much simpler when using C++, rather than C, wrappers, see sections 36.6.2.

## 38.8 C++ Wrappers

When `-c++` command line option is used (and `-nocxx` one is not), the header file generated by SWIG will also contain the declarations of C++ wrapper functions and classes mirroring the original API. All C++ wrappers are fully inline, i.e. don't need to be compiled separately, and are always defined inside the namespace (or nested namespaces) specified by `-namespace` command-line option or the namespace with the same name as the SWIG module name if this option is not specified.

C++ wrappers try to provide a similar API to the original C++ API being wrapped, notably any class `Foo` in the original API appears as a class with the same name in the wrappers namespace, and has the same, or similar, public methods. A class `Bar` deriving from `Foo` also derives from it in the wrappers and so on. There are some differences with the original API, however. Some of them are due to fundamental limitations of the approach used, e.g.:

- Only template instantiations are present in the wrappers, not the templates themselves.

Other ones are due to things that could be supported but haven't been implemented yet:

- Only single, not multiple, inheritance is currently supported.
- Only enums using `int` (or smaller type) as underlying type are supported.

### 38.8.1 Additional customization possibilities

Generated C++ code can be customized by inserting custom code in the following sections:

- `cxxheader` for including additional headers and other declarations in the global scope.
- `cxxcode` for additional code to appear after the declarations of all wrapper classes, inside the module-specific namespace.

The following features are taken into account when generating C++ wrappers:

- `cxxignore` May be set to skip generation of C++ wrappers for the given function or class, while still generating C wrappers for them.

### 38.8.2 Exception handling

Exception handling in C++ is more natural, as the exceptions are re-thrown when using C++ wrappers and so can be caught, as objects of the special `SWIG_CException` type, using the usual `try/catch` statement. The objects of `SWIG_CException` class have `code()` and `msg()` methods, with the latter returning the error message associated with the exception.

If necessary, a custom exception type may be used instead of `SWIG_CException`. To do this, a custom implementation of `swig_check()` function, called to check for the pending exception and throw the corresponding C++ exception if necessary, must be provided and `SWIG_swig_check_DEFINED` preprocessor symbol must be defined to prevent the default implementation of this function from being compiled:

    %insert(cxxheader) %{
    #ifndef SWIG_swig_check_DEFINED
    #define SWIG_swig_check_DEFINED 1

    #include <stdexcept>

    class Exception : public std::runtime_error {
    public:
        explicit Exception(const char* msg) : std::runtime_error{msg} {}
    };

    inline void swig_check() {
      if (auto* swig_ex = SWIG_CException_get_pending()) {
        Exception const e{SWIG_CException_msg_get(swig_ex)};
        SWIG_CException_reset_pending();
        throw e;
      }
    }

    template <typename T> T swig_check(T x) {
      swig_check();
      return x;
    }

    #endif // SWIG_swig_check_DEFINED
    %}