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.h you would run:
This will create a file similar to the following:
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:
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.