load(n)
π load(n) β Tcl Built-In Commands π
okay bestie, this is the load(n) page and it's literally the one that pulls machine code into your Tcl app, so grab a snack π because there's a lot of cute details in here and I'm not skipping a single one.
β¨ NAME β¨
load β load machine code and initialize new commands. that's it, that's the vibe π
π SYNOPSIS π
load ?-global? ?-lazy? ?--? fileName
load ?-global? ?-lazy? ?--? fileName prefix
load ?-global? ?-lazy? ?--? fileName prefix interp
π¦ DESCRIPTION π¦
okay so here's the tea β this command loads binary code from a file into the application's address space and then calls an initialization procedure inside that library so it gets incorporated into an interpreter. iconic behaviour honestly.
fileName is the name of the file holding the code. its exact form varies from system to system (it's giving β¨chaosβ¨) but on most systems it's a shared library β like a .so file under Solaris or a DLL under Windows. prefix is what gets used to compute the name of an initialization procedure. and interp is the path name of the interpreter you want to load the library into (peep the interp manual entry for the deets); if interp is omitted, it defaults to the interpreter in which the load command was invoked. no cap. π«Ά
Once the file has been loaded into the application's address space, one of two initialization procedures gets invoked in the new code. Typically the initialization procedure will add new commands to a Tcl interpreter. The name of that procedure is determined by prefix and by whether or not the target interpreter is a safe one. For normal interpreters the name of the initialization procedure will have the form prefix_Init. For example, if prefix is Foo, the initialization procedure's name will be Foo_Init. π
If the target interpreter is a safe interpreter, then the name of the initialization procedure will be prefix_SafeInit instead of prefix_Init. and listen, the prefix_SafeInit function should be written carefully, so that it initializes the safe interpreter only with partial functionality provided by the library that is safe for use by untrusted code. For more information on Safe-Tcl, see the safe manual entry. π
The initialization procedure must match the following prototype:
typedef int Tcl_LibraryInitProc(
Tcl_Interp *interp);
The interp argument identifies the interpreter in which the library is to be loaded. The initialization procedure must return TCL_OK or TCL_ERROR to indicate whether or not it completed successfully; in the event of an error it should set the interpreter's result to point to an error message. The result of the load command will be the result returned by the initialization procedure. periodt. π«
The actual loading of a file will only be done once for each fileName in an application. If a given fileName is loaded into multiple interpreters, then the first load will load the code and call the initialization procedure; subsequent loads will call the initialization procedure without loading the code again. efficiency queen π
For Tcl versions lower than 8.5, it is not possible to unload or reload a library. From version 8.5 however, the unload command allows the unloading of libraries loaded with load, for libraries that are aware of the Tcl's unloading mechanism. glow up fr fr. π§
The load command also supports libraries that are statically linked with the application, if those libraries have been registered by calling the Tcl_StaticLibrary procedure. If fileName is an empty string, then prefix must be specified. π
If prefix is omitted or specified as an empty string, Tcl tries to guess the prefix by taking the last element of fileName, strip off the first three characters if they are lib, then strip off the next four characters if they are tcl9, and use any following wordchars but not digits, converted to titlecase as the prefix. For example, the command load libxyz4.2.so uses the prefix Xyz and the command load bin/last.so {} uses the prefix Last. the way it just knows π
If fileName is an empty string, then prefix must be specified. The load command first searches for a statically loaded library (one that has been registered by calling the Tcl_StaticLibrary procedure) by that name; if one is found, it is used. Otherwise, the load command searches for a dynamically loaded library by that name, and uses it if it is found. If several different files have been loaded with different versions of the library, Tcl picks the file that was loaded first. first come first served, we love a consistent algorithm π
If -global is specified preceding the filename, all symbols found in the shared library are exported for global use by other libraries. The option -lazy delays the actual loading of symbols until their first actual use. The options may be abbreviated. The option -- indicates the end of the options, and should be used if you wish to use a filename which starts with - and you provide a prefix to the load command. β
On platforms which do not support the -global or -lazy options, the options still exist but have no effect.
β οΈ okay bestie this is the important one: note that use of the-globalor-lazyoption may lead to crashes in your application later (in case of symbol conflicts resp. missing symbols), which cannot be detected during the load. So, only use this when you know what you are doing β you will not get a nice error message when something is wrong with the loaded library. π like, no soft launch, just a crash.
πͺ PORTABILITY ISSUES πͺ
πͺ Windows
When a load fails with library not found error, it is also possible that a dependent library was not found. To see the dependent libraries, type dumpbin -imports <dllname> in a DOS console to see what the library must import. When loading a DLL in the current directory, Windows will ignore ./ as a path specifier and use a search heuristic to find the DLL instead. To avoid this, load the DLL with:
load [file join [pwd] mylib.DLL]
π BUGS π
If the same file is loaded by different fileNames, it will be loaded into the process's address space multiple times. The behavior of this varies from system to system (some systems may detect the redundant loads, others may not). π
πΈ EXAMPLE πΈ
The following is a minimal extension:
#include <tcl.h>
#include <stdio.h>
static int fooCmd(void *clientData,
Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
printf("called with %d arguments\n", objc);
return TCL_OK;
}
int Foo_Init(Tcl_Interp *interp) {
if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
return TCL_ERROR;
}
printf("creating foo command");
Tcl_CreateObjCommand(interp, "foo", fooCmd, NULL, NULL);
return TCL_OK;
}
When built into a shared/dynamic library with a suitable name (e.g. foo.dll on Windows, libfoo.so on Solaris and Linux) it can then be loaded into Tcl with the following:
# Load the extension
switch $tcl_platform(platform) {
windows {
load [file join [pwd] foo.dll]
}
unix {
load [file join [pwd] libfoo[info sharedlibextension]]
}
}
# Now execute the command defined by the extension
foo
π SEE ALSO π
info sharedlibextension, package(n), Tcl_StaticLibrary(3), safe(n)
π KEYWORDS π
binary code, dynamic library, load, safe interpreter, shared library
and that's the whole page bestie, every last crumb of it πβ¨ this one's from the Tcl manual, version 7.5, living under the name load(n) β go forth and load responsibly, and remember: no -global, no -lazy, unless you're built different π
π
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 1995-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms