Compiling a Tcl Script into an Executable

Locating Tcl scripts to load from an executable can be awkward if you want to make your program cross-platform. An easier way is to compile a Tcl script directly into the executable and let that script find any other scripts needed. This is particularly relevent as so many programs just use a single Tcl script to create a Tk GUI and therefore this should be as pain free as possible.

Any easy way around this is to turn your Tcl file into an array that can be included from your source file. This article explores how to do this with Tcl and C.

Converting the Tcl Script

To include the Tcl script it needs to be converted into a C array. This can be done from Unix with the xxd -i command. So to convert my.bin to my.bin.h you would run:

$ xxd -i my.tcl my.tcl.h

If don't have access to xxd, you can use bin2c downloadable as an archive from here. To do as above with bin2c:

$ tclsh bin2c.tcl my.tcl my_tcl my.tcl.h

This will create a file similar to the following:

unsigned char my_tcl[] = {
  0x70, 0x75, 0x74, 0x73, 0x20, 0x22, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c,
  0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x22, 0x0a, 0x70, 0x75, 0x74,
  0x73, 0x20, 0x22, 0x49, 0x20, 0x68, 0x6f, 0x70, 0x65, 0x20, 0x79, 0x6f,
  0x75, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x64, 0x20, 0x74, 0x68, 0x69, 0x73,
  0x20, 0x61, 0x72, 0x74, 0x69, 0x63, 0x6c, 0x65, 0x20, 0x66, 0x72, 0x6f,
  0x6d, 0x3a, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x74, 0x65,
  0x63, 0x68, 0x74, 0x69, 0x6e, 0x6b, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x2e,
  0x63, 0x6f, 0x6d, 0x22, 0x0a
};
unsigned int my_tcl_len = 89;

Loading the Tcl Script

In the example below you can see that my.tcl.h has been included into the function which will load the script. The array created above is then evaluated by Tcl_EvalEx() using the created array: my_tcl, and its associated length variable: my_tcl_len.

#include <tcl.h>

static Tcl_Interp *interp;

int
Script_init()
{
  // Include my.tcl which has been converted to a char array using xxd -i
  #include "my.tcl.h"

  interp = Tcl_CreateInterp();
  if (Tcl_Init(interp) == TCL_ERROR) { return 0; }

  if (Tcl_EvalEx(interp, my_tcl, my_tcl_len, 0) == TCL_ERROR ) {
    fprintf(stderr, "Error in embedded my.tcl\n");
    fprintf(stderr, "%s\n", Tcl_GetStringResult(interp));
    return 0;
  }

  return 1;
}

Conclusion

This process makes it much easier to distribute an executable. Once an initial Tcl script has been loaded, you can use something like the xdgbasedir module to easily locate other scripts for the program. To automate this process, take a look at Using Dynamically Generated Header Files with CMake.

Creative Commons License
Compiling a Tcl Script into an Executable by Lawrence Woodman is licensed under a Creative Commons Attribution 4.0 International License.

Share This Post

Feedback/Discuss

Related Articles

Introducing Ornament a Tcl Template Module

Ornament is a Tcl template module that allows you to define, parse and compile a template to produce a script which can then be run using a safe interpreter. The idea came from the Templates and subst...   Read More

xdgbasedir: A Tcl Module to Access the XDG Base Directory Specification

Unix has traditionally lacked a consistent way of storing user specific and system wide configuration and support files. This has lead to a mess of dot files in a user's home directory and other assoc...   Read More

Embedding an SDL Surface in a Tk Window

Tk is great, but sometimes it just isn't fast enough. SDL is fast, but has no support for input dialogs and other GUI conventions. By embedding an SDL surface in a Tk window you get the best of both ...   Read More

Installing the HI-TECH Z80 C Compiler for CP/M

My language of choice is C and I am currently getting more involved with the CP/M operating system. I have therefore decided that it would be nice to have a C compiler working under CP/M. There are a...   Read More

Modula-2 Compilers on CP/M

Modula-2 is a great language in general and is a good choice for programming on CP/M. There are three good compilers available for CP/M which all require a Z80 processor and we'll compare each in turn...   Read More