Tcl/Tk Cookbook - Adding Extensions


Purpose

This chapter describes how to build a Tcl interpreter that will include the Tcl/Tk exetension libraries of your choice. Without an integrated versions of the tclsh and wish, for each extension you wish to make use, you will be required to run tclsh and wish, usually prepended with the Tcl/Tk extension name, that are compiled specifically for that extension. Otherwise the commands of that Tcl/Tk extension will not be recognised. Tcl/Tk, as we already saw in chapter 6, provides the template for including Tcl/Tk extension libraries and create an integrated version.

You will need

You will require to download, untar and built the source code of the extensions you want to integrate.

Note that if you want to use itcl with C++ as your application programming interface you should compile your extensions with the version of Tcl and Tk libraries in the itcl distribution. To do this build itcl first.

Dish to Serve Up

Create an integrated Tcl_AppInit for all the Tcl/Tk extension libraries BLT and Expect.

You will need

Recipe

The template for this script is tkAppInit.c in the Tcl/Tk source distribution ( look under the directory in which the Tk source code is placed - something like ~pdsrc/TclTk/tk4.0). Make a copy of tkAppInit.c into your current working directory. Call it STclInit.c (This source along with the Makefile template etc. are under ~cookbook/code/ch9)

In the script below, bold letters are used to highlight customised parts in the template code given below:

STclInit.c


#ifndef lint
static char sccsid[] = "@(#) tkAppInit.c 1.12 94/12/17 16:30:56";
#endif /* not lint */
#include "tcl.h"
#include "tk.h"

extern char *exp_argv0; /* For expect */

/*
 * The following variable is a special hack that is needed in order for
 * Sun shared libraries to be used for Tcl.
 */

#ifdef NEED_MATHERR
extern int matherr();
int *tclDummyMathPtr = (int *) matherr;
#endif
/*
 *----------------------------------------------------------------------
 *
 * Tcl_AppInit --
 *
 *	This procedure performs application-specific initialization.
 *	Most applications, especially those that incorporate additional
 *	packages, will have their own version of this procedure.
 *
 * Results:
 *	Returns a standard Tcl completion code, and leaves an error
 *	message in interp->result if an error occurs.
 *
 * Side effects:
 *	Depends on the startup script.
 *
 *----------------------------------------------------------------------
 */


int
Tcl_AppInit(interp)
    Tcl_Interp *interp;		/* Interpreter for application. */
{
    
Tk_Window mainwin;

    mainwin = Tk_MainWindow(interp);

    if (Tcl_Init(interp) == TCL_ERROR) {
	return TCL_ERROR;
    }
    if (Tk_Init(interp) == TCL_ERROR) {
	return TCL_ERROR;
    }

    /*
     * Call the init procedures for included packages.  Each call should
     * look like this:
     *
     * if (Mod_Init(interp) == TCL_ERROR) {
     *     return TCL_ERROR;
     * }
     *
     * where "Mod" is the name of the module.
     */

if (Blt_Init(interp) == TCL_ERROR) {
          return TCL_ERROR;
      }
if (Exp_Init(interp) == TCL_ERROR) {
        return TCL_ERROR;
     }

    /*
     * Call Tcl_CreateCommand for application-specific commands, if
     * they weren't already created by the init procedures called above.
     */

    /*
     * Specify a user-specific startup file to invoke if the application
     * is run interactively.  Typically the startup file is "~/.apprc"
     * where "app" is the name of the application.  If this line is deleted
     * then no user-specific startup file will be run under any conditions.
     */

	tcl_RcFileName ="~/.stclrc";

        return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * main --
 *
 *	This is the main program for the application.
 *
 * Results:
 *	None: Tk_Main never returns here, so this procedure never
 *	returns either.
 *
 * Side effects:
 *	Whatever the application does.
 *
 *----------------------------------------------------------------------
 */

int
main(argc, argv)
    int argc;			/* Number of command-line arguments. */
    char **argv;		/* Values of command-line arguments. */
{
    Tk_Main(argc, argv, Tcl_AppInit);
    return 0;			/* Needed only to prevent compiler warning. */
}


The comment immediately preceeding the first boldface section in the code above gives the exact Tcl syntax for calling init procedures for Tcl/Tk extension modules. This is followed to include BLT_Init (for BLT library with mega widgets and commands) and Exp_Init (for Expect).

A template for the Makefile called Make_stcl is under ~cookbook/code/ch9. Copy this makefile template, customise it for your site and compile STclInit.c by typing:



		make -f Make_stcl

The resultant executable is called stcl. You can invoke this interactively. The result would be similar to (the wish shell is placed inside the window from which it invoked just to capture both windows in one picture):

You can use this shell to execute Expect and or BLT commands. Two very simple applications, one for Expect and one for BLT graph widget are given in the next chapter.