TCL-Language(C/C++) Bindings

Tcl is able to quickly integrate a wide range of technologies, as well as to easily adapt to new technologies and changing end-user requirements. Therefore it is the integration platform of choice for increasing numbers of developers. TCL has in built Language Bindings for C/C++ languages.

C invoking Tcl - an application calling the Tcl interpreter
Tcl extends by C - Tcl calling extensions


Why extend Tcl?

Here are a few reasons:

1. To add domain-specific functionality to Tcl
2. To turn Tcl onto a "better Tcl" - or to support a different programming style
3. To interface the Tcl scripting environment to existing software libraries
4. To hide implementation details by providing only object code releases
5. To rewrite Tcl bottlenecks as C/C++ code for improved performance
6. As a way to incorporate Tcl in an existing body of C/C++ code
7. The last reason is actually the opposite of extending the Tcl environment - in this case, the Tcl interpreter becomes part of an existing application and activated when appropriate

How does a scripting language talk to C?

Scripting languages are built around a small parser that reads and executes statements on the fly as your program runs. Within the parser, there is a mechanism for executing commands or accessing variables. However, in order to access C functions and variables, it is necessary to tell the parser additional information such as the name of the function, what kind of arguments does it take, and what to do when it is called. Unfortunately, this process can be extremely tedious and technical. SWIG automates the process and allows you to forget about it. In any case, it's probably a good idea to know what's going on under the hood.

Wrapper functions

Suppose you have an ordinary C function like this:

int fact(int n)
{
if (n <= 1) return 1; else return n*fact(n-1); } In order to access this function from a scripting language, it is necessary to write a special "wrapper" function that serves as the glue between the scripting language and the underlying C function. A wrapper function must do three things :

1. Gather function arguments and make sure they are valid.
2. Call the C function.
3. Convert the return value into a form recognized by the scripting language.


Links:

http://www.beedub.com/book/3rd/Cprogint.pdf
http://hegel.ittc.ku.edu/topics/tcltk/
http://www.cs.ubc.ca/~heidrich/Papers/TclTk.95.pdf
http://www.mcs.sdsmt.edu/mcgough/tcltk/tcltk.html
http://wiki.tcl.tk/2262
Modular tcl - http://www.amath.washington.edu/~lf/software/tcl++/
C++/Tcl - A C++ library for interoperability between C++ and Tcl - http://cpptcl.sourceforge.net/
http://www.swig.org/Doc1.1/HTML/Scripting.html