Updated 2014-01-26 05:08:26 by SEH
What: SWIG
Where: http://www.swig.org/
Description: Tool designed to make it easier to integrate functions written in C/C++ with Tcl 7 and 8/Tk, Perl 4 and 5, Python and Guile. SWIG is a compiler that takes ANSI C/C++ declarations and builds a scripting language interface for a number of different languages. Works for Unix and Win32.
Currently version 2.0.10 is available.
Updated: 09/2013

As JPF says, Here is a way to call your C and C++ functions from Tcl and other languages.

From http://www.purl.org/NET/Tcl-FAQ/part1.html :

SWIG http://www.swig.org/ is another great resource for using C++ and Tcl. To quote the author:
 > SWIG is a code development tool created to solve real problems and
 > make C/C++ programming more enjoyable. Simply stated, SWIG
 > allows you to integrate common scripting languages such as Tcl,
 > Perl, Python, and Guile with programs containing collections of
 > functions written in C or C++. By using an interpreted scripting
 > language with a C program, you can do a number of cool things like:
 >         Build a powerful interface.
 >         Rapidly prototype new features.
 >         Interactively debug and test your code.
 >         Develop a graphical user interface.
 >         Build C/C++ modules for scripting language applications.
 >         Save lots of time--allowing you to work on the real problem.
 >         Impress your friends.

One user notes:
 > To contrast SWIG with Objectify - SWIG has you prepare a small interface
 > file that specifies what functions are to be wrapped, rather than adding
 > macros to your original header file.  It also works with C, as well as
 > C++.

From http://www.purl.org/NET/Tcl-FAQ/part2.html :

287. The Linux Gazette occasionally covers Tcl related topics, such as the article http://www.linuxgazette.com/issue49/pramode.html "Using SWIG to interface scripting languages with C/C++".
        SWIG: Simplified Wrapper and Interface Generator (SWIG) is an interface
        package which makes it easier to add C code
        to one's Tcl environment (as well as other languages).

        is the location to use for subscription related info, or send mail to
        mailto:Swig-request@cs.uchicago.edu  with the line

        subscribe swig

        An archive of the mailing list can be found at

Author is Dave Beazley.

The idea of Swig is to provide a tool that can, with hopefully little pain, allow one to create glue code between general libraries and various scripting languages, one of which is Tcl. Swig can be used for C++ or C libraries - making it one of the first places people are recommended to look when dealing with legacy libraries.

If you look around in books or places like Cameron Laird's Tcl pages [1] you can find a lot of information about integrating Tcl with C. That's all fine and dandy if you're going to write the interface code by hand.

But if you want a nice example of how you ought to write your wrappers to call C or C++ from Tcl, then you can't go wrong looking at the code generated by SWIG. David Beazley did a great job of implementing both the "old fashioned" Tcl 7.x all-string interface and the "new fangled" Tcl 8.x object interface.

For example, a short little function like this:
double convert ( int *value, char *targetUnits );

Gets wrapped by SWIG with this code for Tcl 8.x:
static int _wrap_convert(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {

        double  _result;
        int * _arg0;
        char * _arg1;
        Tcl_Obj * tcl_result;
        char * rettype;
        int templength;

        clientData = clientData; objv = objv;
        tcl_result = Tcl_GetObjResult(interp);
        if ((objc < 3) || (objc > 3)) {
                Tcl_SetStringObj(tcl_result,"Wrong # args. convert value targetUnits ",-1);
                return TCL_ERROR;
        if ((rettype = SWIG_GetPointerObj(interp,objv[1],(void **) &_arg0,"_int_p"))) {
                Tcl_SetStringObj(tcl_result, "Type error in argument 1 of convert. Expected _int_p, received ", -1);
                Tcl_AppendToObj(tcl_result, rettype, -1);
                return TCL_ERROR;
        if ((_arg1 = Tcl_GetStringFromObj(objv[2], &templength)) == NULL) return TCL_ERROR;
        _result = (double )convert(_arg0,_arg1);
        tcl_result = Tcl_GetObjResult(interp);
        Tcl_SetDoubleObj(tcl_result,(double) _result);
        return TCL_OK;

If you're using SWIG 1.1 or earlier, it also supported the 7.x interface model:
static int _wrap_convert(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[]) {

        double  _result;
        int * _arg0;
        char * _arg1;

        clientData = clientData; argv = argv;
        if ((argc < 3) || (argc > 3)) {
                Tcl_SetResult(interp, "Wrong # args. convert value targetUnits ",TCL_STATIC);
                return TCL_ERROR;
        if (SWIG_GetPtr(argv[1],(void **) &_arg0,"_int_p")) {
                Tcl_SetResult(interp, "Type error in argument 1 of convert. Expected _int_p, received ", TCL_STATIC);
                Tcl_AppendResult(interp, argv[1], (char *) NULL);
                return TCL_ERROR;
        _arg1 = argv[2];
        _result = (double )convert(_arg0,_arg1);
        Tcl_PrintDouble(interp,(double) _result, interp->result);
        return TCL_OK;

The new version SWIG 1.3a5 implements the interface in a different way and no longer supports Tcl 7.x. It also no longer has the excellent automatic documentation package in the older version. What it does have is an excellent macro facility which makes it much easier to interface complicated template classes. JPF

Sorry JPF, but that's several years old. The latest version is 1.3.10 and it rocks, and has great docs.

The current version is 1.3.19. It does have great docs. What it does NOT have is the automatic documentation of a package as a web page which was a feature of the 1.1 version. The same interface file could be used to generate the documentation of the package. This is promised to come back sometime. What it does have is even better support for templates and overloaded operator member functions. JPF 26 June 2003 (back after a long break)

http://www.swig.org/papers/Tcl98/TclChap.html Tcl and SWIG as a C/C++ Development Tool

Swig example showing access to C structures from Tcl

AMG: I'd like the ability to [unload] a Swig extension, but the latest CVS only provides xxx_Init() and xxx_SafeInit(). Any suggestions?

In February, 1998 issue of Dr. Dobbs Journal, David Beazley, creator of SWIG, writes an intro to SWIG.

AM Here is a page on a similar tool: Generating wrappers for C and Fortran

hae 2008/11/18 The french Tcl-Wiki has some good examples: http://wfr.tcl.tk/1693

2011/04/13: David Beazley: "... life is too short to wrap my brain around the ever-growing pile of hacks called C++. ... things like this are why I don't work on swig anymore."

As of SWIG 1.3.40, the generated C/C++ wrapper will use the Stubs feature if compiled with -DUSE_TK_STUBS. Also, you can override the minimum version to support which is passed to Tcl_InitStubs() and Tk_InitStubs() with -DSWIG_TCL_STUBS_VERSION="8.3" or the version being compiled with using -DSWIG_TCL_STUBS_VERSION=TCL_VERSION.