A sample dynamically loadable extension for Tcl 7.5

by Scott Stanton w/ help from Ray Johnson
scott.stanton@eng.sun.com
ray.johnson@eng.sun.com

SCCS: @(#) README 1.6 96/04/26 11:36:47

1. Introduction
---------------

Version 7.5 of Tcl now has the ability to dynamically load
binary Tcl extensions at run time.  See the load command for
details on how to do this from Tcl.  This distribution provides
a very simple example on how to build a binary Tcl extension
on the various major platforms.

This directory contains a sample dynamically loadable extension for
Tcl 7.5.  This extension demonstrates the techniques needed to add a
new command to Tcl in a cross platform manner.  You should be able to
use this example as a template for building your own extensions.

2. Compiling the example
------------------------

For Unix systems:

   (a) Make sure that the Tcl 7.5 release is present in the directory
       ../tcl7.5.  This example will only work with Tcl 7.5.  Also, be
       sure that you have configured Tcl before you configure the
       example.  You can obtain the Tcl 7.5 release by anonymous ftp
       from ftp.sunlabs.com:/pub/tcl.

   (b) Type "./configure".  This runs a configuration script created
       by GNU autoconf, which configures Tcl for your system and
       creates a Makefile.  The configure script allows you to
       customize the Tcl configuration for your site; for details on
       how you can do this, type "./configure -help" or refer to the
       autoconf documentation (not included here).  Note: the example
       will use the same C compiler (e.g. gcc) as Tcl, as well as
       several other of Tcl's configuration options.  You should not
       usually modify CC in the Makefile generated here (if do you, be
       sure to check all of the definitions related to dynamic loading
       to make sure that they are still correct).  The example
       "configure" supports the following special switches in addition
       to the standard ones:
	   --with-tcl=DIR	Specifies the directory containing the Tcl
				binaries and Tcl's platform-dependent
				configuration information.  By default
				the Tcl directory is assumed to be in
				the location given by (a) above.

   (c) Type "make".  This will create a shared library called
       "example.so" and a package index file called "pkgIndex.tcl".

   (d) Type "make test" to test the example.  This will attempt to
       load the example package from the current directory.  If it
       succeeds, you will see the message "Test passed.", otherwise
       you will see an error.

For Microsoft Windows systems:

   (a) Make sure that you have installed the Tcl 7.5 binary release.
       Because most "make" programs don't support long file names, you
       should install Tcl into a directory that does not contain
       spaces in the name.  This is primarily an issue under Windows
       95, since the default location is under the "Program Files"
       directory.

   (b) If you are using the Borland compilers:

	1. Copy the file "makefile.bc" to the file "makefile".

	2. Edit "makefile" and change the BORLAND and TCL macros to
	   point to the directories where the Borland and Tcl releases
	   are installed.

	3. Type "make".  This will create a DLL called "example.dll"
	   and a package index file called "pkgIndex.tcl".

   (c) If you are using the Visual C++ compilers:

	1. You will need the file "vclibs41.zip" from the
	   ftp.sunlabs.com:/pub/tcl archive.  This file contains the
	   .LIB import files needed to link against the binary release
	   of Tcl 7.5.

	2. Unzip the vclibs41.zip archive into the current directory.
	   This should create the tcl75.lib and tk41.lib files.

	3. Copy the file "makefile.vc" to the file "makefile".

	4. Edit "makefle" and change the TOOLS32 and TCL variables to point
	   to the directories where the Visual C++ and Tcl releases are
	   installed.

	5. Type "nmake".  This will create a DLL called "example.dll"
	   and a package index file called "pkgIndex.tcl".

	6. Type "nmake test" to test the example.  This will attempt
	   to load the example package from the current directory.  If
	   it succeeds, you will see the message "Test passed.",
	   otherwise you will see an error.

    (d) You can install the example by placing the "example.dll" file
	in the directory containing tclsh75.exe or a directory on your
	path.  In addition, you should place the pkgIndex.tcl file
	into a directory on your auto_path.

For Macintosh systems:

    Before you can compile the example, you must have the Tcl 7.5
    source release.  You can obtain the release by anonymous ftp from
    ftp.sunlabs.com:/pub/tcl/mac.  You should install the directory
    including this example at the same level as the Tcl 7.5 workspace.
    Once you've done that, you should be able to use the included
    project file as is.
    
    Note: Dynamic loading only works on Power PC Macs.

3. Porting Issues
-----------------

Because the binary release of Tcl for Windows is compiled with Borland
C++ 4.52, the malloc() and free() routines it uses are not compatible
with the malloc() and free() supplied by other C run time libraries
(either VC++ or any other compiler).  In order to avoid memory
corruption, you must never pass malloc'ed memory to Tcl, expecting Tcl
to use free() on it.  Instead you need to pass your own free proc
along with the data.  This will mostly affect uses of Tcl_SetResult
(or interp->result) and the TCL_DYNAMIC flag.  Similarly, you should
not attempt to free memory that Tcl has allocated.

Eventually, we plan to supply a Tcl_Alloc and Tcl_Free routine that
can be used by Tcl extensions to safely allocate/deallocate memory
using the Tcl allocator, regardless of the compiler used.
