Next: , Previous: , Up: C Interface   [Contents][Index]


5.26.9 Automated interface generation using SWIG

SWIG, the Simple Wrapper Interface Generator, is used to create C interfaces for a lot of programming languages. The SWIG version extended with a Forth module can be found on github.

5.26.9.1 Basic operation

C-headers are parsed and converted to Forth-Sourcecode which uses the previously describe C interface functions.

5.26.9.2 Detailed operation:

  1. Select a target, in this example we are using example.h
  2. Create an interface file for the header. This can be used to pass options, switches and define variables. In the simplest case it just instructs to translate all of example.h:
    %module example
    %insert("include")
    {
        #include "example.h"
    }
    %include "example.h"
    
  3. Use SWIG to create a .fsi-c file.
    swig -forth -stackcomments -use-structs -enumcomments -o example-fsi.c example.i.
    FSI stands “Forth Source Independent” meaning it can be transferred to any host having a C-compiler. SWIG is not required past this point.
  4. On the target machine compile the .fsi-c file to a .fsx (x stands for executable)
    gcc -o example.fsx example-fsi.c
    The compilation will resolve all constants to the values on the target.
  5. The last step is to run the executable and capture its output to a .fs “Forth Source” file.
    ./example.fsx -gforth > example.fs
    This code can now be used on the target platform.

5.26.9.3 Examples

You can find some examples in SWIG’s Forth Example section.

A lot of interface files can be found in Forth Posix C-Interface and Forth C-Interface Modules.

Contribution to the Forth C-Interface Module repository is always welcome.


Next: , Previous: , Up: C Interface   [Contents][Index]