Version 18 of Adding Tcl/Tk to a C application

Updated 2003-07-09 09:44:47

The following questions tend to be asked regularly on the comp.lang.tcl newsgroup:

  • I have a large C/C++ program, how do I make it scriptable with Tcl?
  • I have a large C/C++ program, how do I add a Tk GUI to it?

At a high level, there are three different solutions to consider.

  1.  Embed Tcl calls in my C/C++ code.

  2.  Wrap my C/C++ code to make it callable from Tcl.
      Replace the C/C++ main() with a tclsh/wish main program

  3.  Something else

Embed Tcl Calls in C/C++ code

While the text "Why adding Tcl calls to a C/C++ application is a bad idea" is an interesting dialog concerning the appropriate approach to performing this task, where can one find specific coding examples and documentation regarding what one needs to do, in a C program, if one needs to create an interpreter and then execute tcl commands making use of that interpreter.

I do know there are two kinds of Tcl actions one can invoke from C/C++ Since Tcl is just a C library, some Tcl actions can be invoked by calling the appropriate Tcl function call.

However, some things in Tcl must be done by invoking Tcl_Eval, after appropriately setting up a Tcl interpreter.

Can someone provide some sample C or C++ code that shows setting up the interpreter, then perhaps invoking things each way?

A dream scenario would do this for Tcl/Tk since that is a superset of the same kind of request for Tcl...