an9wer

Invoking Tcl in C

Posted:

2023-12-08

In my previous post, I attempted to utilize a command-line tool - rsstail - to notify me of news from Slashdot, but I eventually decided to write my own scripts to monitor Slashdot's RSS feeds and detect new entries. Also, I preferred to use the Tcl programming language for this task due to its simplicity and portability.

The only challange is that Tcl doesn't natively support running as a daemon in the background, unless additional libraries (e.g., tcllauncher) or TclX (Extended Tcl) are used. However, since Tcl is written in C and provides C APIs, I can use C to fork a Tcl process to make it a deamon.

Writing C to invoke Tcl

Once my script has been implemented in Tcl, the next step is to invoke it from C, which involves several C APIs.

The first API function,``Tcl_CreateInterp``, creates a Tcl interpreter structure, which is then used as a parameter for other C APIs:

Tcl_Interp *interp = Tcl_CreateInterp();

After creating the Tcl interpreter, the next API function Tcl_Init is called to load the script "init.tcl" from somewhere on the Tcl library path, setting up the script library facility. This enables me to properly import packages in my Tcl script (e.g., package require http):

Tcl_Init(interp);

Lastly, call the API function Tcl_EvalFile to load and execute my Tcl script:

Tcl_EvalFile(interp, <FILENAME>);

Compiling C codes

When compiling C code that uses Tcl, I have to specify both the include path and the library path. The include path should point to the directory containing "tcl.h" (e.g., "/usr/local/include/tcl8.6"), and the library path should point to the directory containing "libtcl.so" (e.g., "/usr/local/lib"). Additionally, some Tcl packages, such as http, require linking against the pthread library. If omitted, I may encounter an error like undefined symbol 'pthread_attr_init':

cc -Wl,-L,/usr/local/lib,-l,tcl86,-l,pthread -I/usr/local/include/tcl8.6 <C FILES>

Thanks for reading :)