CommandName: load ManualSection: n Version: 7.5 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:
- info sharedlibextension
- package(n)
- Tcl_StaticLibrary(3)
- safe(n) Keywords:
- binary code
- dynamic library
- load
- safe interpreter
- shared library Copyright:
- Copyright (c) 1995-1996 Sun Microsystems, Inc. ---
Name
load - Load machine code and initialize new commands
Synopsis
::: {.synopsis} ::: [load]{.cmd} [-global]{.optlit} [-lazy]{.optlit} [--]{.optlit} [fileName]{.arg} [load]{.cmd} [-global]{.optlit} [-lazy]{.optlit} [--]{.optlit} [fileName]{.arg} [prefix]{.arg} [load]{.cmd} [-global]{.optlit} [-lazy]{.optlit} [--]{.optlit} [fileName]{.arg} [prefix]{.arg} [interp]{.arg} :::
Description
This command loads binary code from a file into the application's address space and calls an initialization procedure in the library to incorporate it into an interpreter. fIfileNamefR is the name of the file containing the code; its exact form varies from system to system but on most systems it is a shared library, such as a fB.sofR file under Solaris or a DLL under Windows. fIprefixfR is used to compute the name of an initialization procedure. fIinterpfR is the path name of the interpreter into which to load the library (see the fBinterpfR manual entry for details); if fIinterpfR is omitted, it defaults to the interpreter in which the fBloadfR command was invoked.
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 the initialization procedure will add new commands to a Tcl interpreter. The name of the initialization procedure is determined by fIprefixfR and whether or not the target interpreter is a safe one. For normal interpreters the name of the initialization procedure will have the form fIprefixfRfB_InitfR. For example, if fIprefixfR is fBFoofR, the initialization procedure's name will be fBFoo_InitfR.
If the target interpreter is a safe interpreter, then the name of the initialization procedure will be fIprefixfRfB_SafeInitfR instead of fIprefixfRfB_InitfR. The fIprefixfRfB_SafeInitfR 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 fBsafefR manual entry.
The initialization procedure must match the following prototype:
typedef int Tcl_LibraryInitProc(
Tcl_Interp *interp);
The fIinterpfR argument identifies the interpreter in which the library is to be loaded. The initialization procedure must return fBTCL_OKfR or fBTCL_ERRORfR 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 fBloadfR command will be the result returned by the initialization procedure.
The actual loading of a file will only be done once for each fIfileNamefR in an application. If a given fIfileNamefR is loaded into multiple interpreters, then the first fBloadfR will load the code and call the initialization procedure; subsequent fBloadfRs 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 fBunloadfR command allows the unloading of libraries loaded with fBloadfR, for libraries that are aware of the Tcl's unloading mechanism.
The fBloadfR command also supports libraries that are statically linked with the application, if those libraries have been registered by calling the fBTcl_StaticLibraryfR procedure. If fIfileNamefR is an empty string, then fIprefixfR must be specified.
If fIprefixfR is omitted or specified as an empty string, Tcl tries to guess the prefix by taking the last element of fIfileNamefR, strip off the first three characters if they are fBlibfR, then strip off the next four characters if they are fBtcl9fR, and use any following wordchars but not digits, converted to titlecase as the prefix. For example, the command fBload libxyz4.2.sofR uses the prefix fBXyzfR and the command fBload bin/last.so {}fR uses the prefix fBLastfR.
If fIfileNamefR is an empty string, then fIprefixfR must be specified. The fBloadfR command first searches for a statically loaded library (one that has been registered by calling the fBTcl_StaticLibraryfR procedure) by that name; if one is found, it is used. Otherwise, the fBloadfR command searches for a dynamically loaded library by that name, and uses it if it is found. If several different files have been fBloadfRed with different versions of the library, Tcl picks the file that was loaded first.
If fB-globalfR is specified preceding the filename, all symbols found in the shared library are exported for global use by other libraries. The option fB-lazyfR delays the actual loading of symbols until their first actual use. The options may be abbreviated. The option fB--fR indicates the end of the options, and should be used if you wish to use a filename which starts with fB-fR and you provide a prefix to the fBloadfR command.
On platforms which do not support the fB-globalfR or fB-lazyfR options, the options still exist but have no effect. Note that use of the fB-globalfR or fB-lazyfR option may lead to crashes in your application later (in case of symbol conflicts resp. missing symbols), which cannot be detected during the fBloadfR. 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 fBloadfRed by different fIfileNamefRs, 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. fBfoo.dllfR on Windows, fBlibfoo.sofR 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