Version 1 of Tcl and C, extending and embedding

Updated 2023-07-14 14:27:31 by torsten

General information on Tcl and C

Tcl can be regarded as a powerful configuration language sitting on top of a bunch of C functions. Tcl's original purpose (according to Tcl's inventor John Ousterhout (see How do I use the Tcl C API?)), was to create a standard language that could be embedded in applications. The intent was that Tcl would be accessed only via its C API.

Extending Tcl

This C API can be use to extend Tcl, giving it new commands and new functionality (see Extending Tcl). The recommended way for this is to use Tcl's Extension Architecture TEA, a set of guidelines and techniques for the distribution, configuration, compilation, and installation of Tcl extensions. TEA comes with a "sample extension" showing how to do things:

Regardless whether you are writing an extension in C or Tcl, here is some more information:

Hello world

To see how extendind Tcl works in practice, you should have a look at the typical 'Hello world' example:

There are also other examples of small extensions in C:

More on TEA

The Tcl Extension Architecture is a whole story in itself. TEA is largely defined by the tcl.m4 file in tclconfig, along with the configure.in and Makefile files in the sample extension. A makefile.vc and rules.vc file is also included for Windows builds. TEA describes a directory layout both for the source distribution and for files installed as part of the extension. Files in the source distribution of an extension are laid out such that platform-specific components live in a directory dedicated to that platform. The version of TEA is defined by the TEA_VERSION variable in the tcl.m4 file in tclconfig.

Here are some pages dealing with TEA:

Examples of existing extensions using TEA are: Tktable, TclX, itcl/itk.

Extending Tcl (with C) from within Tcl

Going one step further, you can even extend Tcl with a C function that is written inside the Tcl script where you need it:

Embedding Tcl

Of course, you can also still use Tcl as embedded language in you application. Here are some pages about this, including customization of tclsh itself:

Embedding Tcl typically involves using the C API function Tcl_Init.

The Tcl C API

All the Tcl C API functions have manual pages. However, the Wiki has also a bit of information on some of the API functions. Start with the list of function here:

There are some commin design principles in the C API which you read about here: Tcl C API Design Principles.

Some special information on certain aspects of the Tcl C API can be found here:

About the string interface and the Tcl_Obj interface

The Tk C API

Just as Tcl, also Tk has its own C API. Here is a list (probably incomplete) with more information on at least a few of the Tk C functions:

Here is some more information (and examples) on extending Tk: