SWIG

from Wikipedia, the free encyclopedia

SWIG ( S implified W rapper and I nterface G enerator ) is a programming tool , which in C or C ++ viewing modules for other programming languages , in particular scripting languages , makes available.

SWIG is an open source project and can be used independently of the operating system (cross-platform).

As source languages are C and C ++ , as the target languages, scripting languages, Tcl , Perl , Python , Ruby , PHP , Lua and furthermore also Java , C # , Scheme , Ocaml , R and Go supported.

functionality

The multitude of modern programming languages ​​can only be used meaningfully because all of these languages ​​can access a wide range of already existing software bundled in program libraries. Operating system-related libraries are mainly written in C. One possibility of integrating C libraries is therefore part of the basic scope of every universally applicable script language; a language must have such a mechanism in order to be included as a target language in SWIG.

The integration is usually carried out in such a way that the source code of the original library remains unchanged and additional C functions, so-called wrappers , are created. These wrappers serve as an interface between the library and the target language and can be called from the target language, take parameters from the target language and return results to it. Since the wrapper functions only adapt the interface, they in turn call the corresponding function of the output library. In order to achieve easy use in the target language, it is usually also necessary to encapsulate the calls in this language using constant definitions and / or wrappers.

SWIG largely automates this process. You only have to add a few specific instructions to the header file of the source library or include it in a SWIG-specific super header. SWIG then generates C source code that contains the wrappers for all functions of the source library. In addition, the required wrapper code for the target language is generated. For Java z. B. the calls via the JNI .

No access to the source code of the source library is required to generate the wrapper. The wrappers are compiled with a platform-specific compiler and either linked to the source library or written to a separate library.

Since Swig is just a code generator and the code is written into the library by the compiler / linker, no further runtime environment is required when using the target language .

Limits

The limits of SWIG are essentially determined by the target language. It concerns all language elements of C ++ or C that cannot be represented there meaningfully. One example is the multiple inheritance of C ++, which hardly exists in other languages. In these cases, it may be necessary to adapt the source code of the original library or to encapsulate it again.

Web links