table of contents
other languages
other sections
conflicting packages
load(3tcl) | Tcl Built-In Commands | load(3tcl) |
NAME¶
load - Load machine code and initialize new commandsSYNOPSIS¶
load fileNameDESCRIPTION¶
This command loads binary code from a file into the application's address space and calls an initialization procedure in the package to incorporate it into an interpreter. fileName 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 .so file under Solaris or a DLL under Windows. packageName is the name of the package, and is used to compute the name of an initialization procedure. interp is the path name of the interpreter into which to load the package (see the interp manual entry for details); if interp is omitted, it defaults to the interpreter in which the load 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 packageName and whether or not the target interpreter is a safe one. For normal interpreters the name of the initialization procedure will have the form pkg_Init, where pkg is the same as packageName except that the first letter is converted to upper case and all other letters are converted to lower case. For example, if packageName is foo or 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 pkg_SafeInit instead of pkg _Init. The pkg_SafeInit function should be written carefully, so that it initializes the safe interpreter only with partial functionality provided by the package 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_PackageInitProc(Tcl_Interp * interp);
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(ClientData 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; }
# 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, Tcl_StaticPackage(3tcl), safe(3tcl)KEYWORDS¶
binary code, loading, safe interpreter, shared library7.5 | Tcl |