load(3tcl)
π load β bestie, we're loading machine code and it's giving main character energy π
okay so this is load from Tcl Built-In Commands, and it's literally the command that yeets compiled machine code into your app and wakes up a little init function to make new commands appear. no cap. let's get into it π¦
β¨ SYNOPSIS β¨
load ?-global? ?-lazy? ?--? fileName
load ?-global? ?-lazy? ?--? fileName prefix
load ?-global? ?-lazy? ?--? fileName prefix interp
πΈ DESCRIPTION πΈ
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. slay.
fileName is the name of the file holding the code. its exact form varies from system to system, but on most systems it's a shared library β think a .so file under Solaris, or a DLL under Windows. iykyk.
prefix is used to compute the name of an initialization procedure.
interp is the path name of the interpreter into which to load the library (peep the interp manual entry for details). if interp is omitted, it defaults to the interpreter in which the load command was invoked. periodt.
Once the file has been loaded into the application's address space, one of two initialization procedures will be invoked in the new code. Typically that init procedure will add new commands to a Tcl interpreter. The name of the initialization procedure is determined by prefix and whether or not the target interpreter is a safe one.
- For normal interpreters, the init procedure's name has the form
prefix_Init. So ifprefixisFoo, the initialization procedure's name will beFoo_Init. iconic. - If the target interpreter is a safe interpreter, the name will be
prefix_SafeInitinstead ofprefix_Init. Theprefix_SafeInitfunction 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 info on Safe-Tcl, see thesafemanual entry. π«Ά
The initialization procedure must match this 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. π
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. 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 Tcl's unloading mechanism. π
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, stripping off the first three characters if they are lib, then stripping off the next four characters if they are tcl9, and using 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 that it just figures it out?? obsessed. π₯
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. fr fr.
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.
β οΈ lowkey be careful bestie: on platforms which do not support the-globalor-lazyoptions, the options still exist but have no effect. 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. π
π 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). not me finding this out the hard way π
β¨ 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 sharedlibextensionpackage(3tcl)Tcl_StaticLibrary(3tcl)safe(3tcl)
π KEYWORDS π
binary code, dynamic library, load, safe interpreter, shared library
okay bestie that's the whole page, you ate that π
β¨ this one's from Tcl 7.5, and it's the load(3tcl) page β go forth and load responsibly, mwah π«Άπ
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- 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