But shared libraries are much more convenient when distributed using the Tcl package facility. Using this mode, Critcl will generate a Tcl package structure (including the pkgIndex.tcl file). The structure is arranged so that the results of invocations on different architectures are accumulated in platform specific directories and when run, the pkgIndex.tcl file loads the shared library for the current platform. These package structures are useful for creating cross-platform applications - such as Starkits that may be run on several architectures.
So, let's update our mymath.tcl with a "package provide" command:
package require critcl package provide mymath 1.0
critcl::cproc noop {} void {}
critcl::cproc add {int x int y} int { return x + y; }
critcl::cproc cube {int x} int { return x * x * x; }
(Note that the .tcl extension is optional on the Critcl command line).
Copying the lib/mymath directory to somewhere on Tcl's auto_path, or to the lib directory of a Starkit, will allow the mymath package to be loaded as follows:
$ tclkit % package require mymath 1.0 % add 1 2 3
As we have seen, CriTcl generates a lib/mymath directory next to the mymath.tcl source. Looking inside this we see:
The critcl.tcl file is more complex - it provides dummy definitions for each Critcl procedure (in case they are called from the other scripts) plus two key procedures:
critcl::loadlib which is used by pkgIndex.tcl to load the appropriate shared library and any Tcl files
critcl::platform which generates a normalised name for each platform (needed because the contents of tcl_platform array are not always consistent across different architectures)
Note also that there is a platform specific directory containing the shared library, plus a small critcl.tcl script that stores the values of certain Critcl parameters from the build process, in case they are referred to by the package Tcl script.
As the package is built on multiple platforms, Critcl will add similar directories. For example, if we copy mymath.tcl and the lib directory to a Linux platform and run Critcl again:
Critcl takes care to store the cached and intermediate files in separate per-user/per-platform directories, so it would be safe to have both the mymath source and the user's home directory on a shared network drive.