Compiling a Tcl Script into an Executable

  /     /   C     Programming     Tcl/Tk     Tutorial    

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;

  // 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;


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.

Related Articles

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

Adding a Basic Stub to a Vic-20 Assembly Language Program

To make machine language programs more friendly it is nice to add a basic stub which contains a line with a SYS statement to start the code. This is easy to do on the Vic-20 and the process gives you ...   Read More

Beginning Assembly Programming on the Commodore Vic-20

The Commodore Vic-20 is a great machine to learn an assembly language on. It was released in 1981 and was the first computer to sell one million units, which contributes to its popularity today. The ...   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

Sign up to get new articles straight to your inbox.

Delivered by FeedBurner


blog comments powered by Disqus