How to Compile Executables With GCC That Include the Tcl and Tk Libraries


Overview
This describes how to make executables that do not need Tcl and Tk shared libraries to run.
 

Expand Overview
This document describes how to use Mktclapp to convert a Tcl and Tk script to C code, then compile the executable with static libraries.
 

You will need Mktclapp to convert your Tcl scripts to C.
http://www.hwaci.com/sw/mktclapp/

Download the free Tcl and Tk sources.
http://www.scriptics.com

First remove any existing Tcl and Tk distribution, and the header files for Tcl and Tk in /usr/include.

Extract the sources so that tcl8.2.2 and tk8.2.2 are in the same directory.
Get any extensions you want to use and apply the patches to the Tcl or Tk sources.

Add /usr/local/lib to your /etc/ld.so.conf, if it does not already exist within that file.  Now as root or via su run ldconf to update the ld path.

Now change to the tcl8.2.2/unix directory.

Run ./configure --disable-shared and let it do its magic.

Now run make to build tclsh8.2 and the static libtcl8.2.a library.

With root access or root access via su run make install.

Now change to the tk8.2.2/unix directory.

Run ./configure --disable-shared and let it run.

Now run make to build wish8.2 and the static libtk8.2.a library.

With root access or root access via su run make install.

Now convert your Tcl script to C code with Mktclapp following these instructions.
http://www.hwaci.com/sw/mktclapp/mktclapp.html

Try building your C code with something like this:
gcc filename.c /usr/local/lib/libtk8.2.a /usr/local/lib/libtcl8.2.a -L/usr/X11R6/lib -lX11 -lm -ldl -o filename

You will notice how the full path to the library is declared.  It should compile without any problems.

Now if you run ldd filename it should report something like this:

libX11.so.6 => /usr/X11R6/lib/libX11.so.6 (0x4001e000)
libm.so.6 => /lib/libm.so.6 (0x400c2000)
libdl.so.2 => /lib/libdl.so.2 (0x400e1000)
libc.so.6 => /lib/libc.so.6 (0x400e4000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

The ldd program reports what shared libraries are required by an executable.

If you do have problems try upgrading to a newer version of Tcl and Tk.  I experienced linking problems with Tcl 8.2.1 which were fixed by an upgrade to Tcl 8.2.2.  Also try linking only the Tcl library as static and the Tk library as shared and vice versa, so that you can narrow down the problem.  You will need to create a shared library for this, by running configure without any options when you build Tcl and Tk.

For example to link with a shared Tk and static Tcl use something like this.
gcc filename.c -ltk8.2 /usr/local/lib/libtcl8.2.a -L/usr/X11R6/lib -lX11 -lm -ldl -o filename
 

It may take some time to learn how to do this.  It took me roughly two weeks of off and on effort to learn how to do this.  I tried using the -static and -shared options while compiling, but when I did that it caused segmentation faults.  So I finally tried just declaring the library and it worked.  Do you know why it caused problems?  Why does the ld man page list -Bstatic and the gcc man page list -static?

Well, I hope that this has been useful.  If you see a problem that I have overlooked, or have a suggestion to improve the clarity of this please tell me about it.