Version 20 of Swig

Updated 2006-11-26 22:36:50

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

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++.

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).
        http://www.cs.uchicago.edu/mailman/listinfo/swig 

        http://www.cs.uchicago.edu/mailman/options/swig/ 
        is the location to use for subscription related info, or send mail to
        mailto:[email protected]  with the line

        subscribe swig

        An archive of the mailing list can be found at
        http://www.swig.org/Archive/archives.html

 What: SWIG
 Where: http://swig.sourceforge.net/
        http://swig.cs.uchicago.edu/swig-1.3.12c1.tar.gz
        ftp://swig.sourceforge.net/pub/swig/MacSWIG1.1p2.sea.hqx
        ftp://ftp.caos.kun.nl/pub/misc/jswig.tar.gz
        ftp://ftp.caos.kun.nl/pub/misc/jswig/jswig1.2a3.tar.gz
        ftp://ftp.caos.kun.nl/pub/misc/jswig/jswig.1.3a3.zip
        ftp://astro.phys.cmu.edu/pub/ptak/mac_swig_example/
        http://starship.skyport.net/crew/robind/python/
        http://www.neurop2.ruhr-uni-bochum.de/%7Ecozzi/swigeiffel/
        http://starship.skyport.net/crew/robind/python#swig
        http://www.techentin.net/tcl/
        http://superk.physics.sunysb.edu/%7Emcgrew/guile
        http://www.goto.info.waseda.ac.jp/%7Efukusima/ruby/swig-e.html
        http://sourceforge.net/patch/?func=detailpatch&patch_id=101430&group_id=1645
        http://www.geocities.com/digitalshmoo/dev/swig/
 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.
        The SWIG documentation and a tutorial are available on SourceForge.
        Jswig is an extension by Harco de Hilster which generates
         Java native code classes from the SWIG templates.
         Should be extracted into SWIG1.1/Examples/java/.
        Example of getting SWIG working with Macintosh, by Andy Ptak,
         at CMU.  There are also SWIG/python patches available by
         Robin Dunn.  SWIG interface for Eiffel has been made available
         by Alex Cozzi, who provides a version for SWIG 1.1p5 as well as one
         for SWIG 1.2a1.
        The techentin site contains patches to SWIG 1.1-850 so that swig
         can generate [incr tcl] shadow classes.
        The waseda site has a SWIG module for Ruby.
        The sourceforge patch reference provides Guile 1.3.4 support.
        The digitalsmoo site contains a sample of using SWIG to create
         shadow objects for C++ classes wrapped by perl.
        The sf.net site is now the official repository for swig release tars.
        Currently version 1.3.29 is available.
 Updated: 05/2002
 Contact: mailto:[email protected] (David Beazley)
        mailto:[email protected] (Harco de Hilster)
        mailto:[email protected] (Andy Ptak)

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 [L1 ] 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?


Category Application