1load(n) Tcl Built-In Commands load(n)
2
3
4
5______________________________________________________________________________
6
8 load - Load machine code and initialize new commands.
9
11 load fileName
12 load fileName packageName
13 load fileName packageName interp
14_________________________________________________________________
15
16
18 This command loads binary code from a file into the application's
19 address space and calls an initialization procedure in the package to
20 incorporate it into an interpreter. fileName is the name of the file
21 containing the code; its exact form varies from system to system but
22 on most systems it is a shared library, such as a .so file under
23 Solaris or a DLL under Windows. packageName is the name of the pack‐
24 age, and is used to compute the name of an initialization procedure.
25 interp is the path name of the interpreter into which to load the pack‐
26 age (see the interp manual entry for details); if interp is omitted, it
27 defaults to the interpreter in which the load command was invoked.
28
29 Once the file has been loaded into the application's address space, one
30 of two initialization procedures will be invoked in the new code. Typ‐
31 ically the initialization procedure will add new commands to a Tcl
32 interpreter. The name of the initialization procedure is determined by
33 packageName and whether or not the target interpreter is a safe one.
34 For normal interpreters the name of the initialization procedure will
35 have the form pkg_Init, where pkg is the same as packageName except
36 that the first letter is converted to upper case and all other letters
37 are converted to lower case. For example, if packageName is foo or
38 FOo, the initialization procedure's name will be Foo_Init.
39
40 If the target interpreter is a safe interpreter, then the name of the
41 initialization procedure will be pkg_SafeInit instead of pkg_Init. The
42 pkg_SafeInit function should be written carefully, so that it initial‐
43 izes the safe interpreter only with partial functionality provided by
44 the package that is safe for use by untrusted code. For more informa‐
45 tion on Safe-Tcl, see the safe manual entry.
46
47 The initialization procedure must match the following prototype:
48 typedef int Tcl_PackageInitProc(Tcl_Interp *interp);
49 The interp argument identifies the interpreter in which the package is
50 to be loaded. The initialization procedure must return TCL_OK or
51 TCL_ERROR to indicate whether or not it completed successfully; in the
52 event of an error it should set the interpreter's result to point to an
53 error message. The result of the load command will be the result
54 returned by the initialization procedure.
55
56 The actual loading of a file will only be done once for each fileName
57 in an application. If a given fileName is loaded into multiple inter‐
58 preters, then the first load will load the code and call the initial‐
59 ization procedure; subsequent loads will call the initialization pro‐
60 cedure without loading the code again. It is not possible to unload or
61 reload a package.
62
63 The load command also supports packages that are statically linked with
64 the application, if those packages have been registered by calling the
65 Tcl_StaticPackage procedure. If fileName is an empty string, then
66 packageName must be specified.
67
68 If packageName is omitted or specified as an empty string, Tcl tries to
69 guess the name of the package. This may be done differently on differ‐
70 ent platforms. The default guess, which is used on most UNIX plat‐
71 forms, is to take the last element of fileName, strip off the first
72 three characters if they are lib, and use any following alphabetic and │
73 underline characters as the module name. For example, the command load
74 libxyz4.2.so uses the module name xyz and the command load bin/last.so
75 {} uses the module name last.
76
77 If fileName is an empty string, then packageName must be specified. │
78 The load command first searches for a statically loaded package (one │
79 that has been registered by calling the Tcl_StaticPackage procedure) by │
80 that name; if one is found, it is used. Otherwise, the load command │
81 searches for a dynamically loaded package by that name, and uses it if │
82 it is found. If several different files have been loaded with differ‐ │
83 ent versions of the package, Tcl picks the file that was loaded first.
84
86 Windows
87 When a load fails with "library not found" error, it is also
88 possible that a dependent library was not found. To see the
89 dependent libraries, type ``dumpbin -imports <dllname>'' in a
90 DOS console to see what the library must import. When loading a
91 DLL in the current directory, Windows will ignore ``./'' as a
92 path specifier and use a search heuristic to find the DLL
93 instead. To avoid this, load the DLL with:
94 load [file join [pwd] mylib.DLL]
95
97 If the same file is loaded by different fileNames, it will be loaded
98 into the process's address space multiple times. The behavior of this
99 varies from system to system (some systems may detect the redundant
100 loads, others may not).
101
103 The following is a minimal extension:
104
105 #include <tcl.h>
106 #include <stdio.h>
107 static int fooCmd(ClientData clientData,
108 Tcl_Interp *interp, int objc, char * CONST objv[]) {
109 printf("called with %d arguments\n", objc);
110 return TCL_OK;
111 }
112 int Foo_Init(Tcl_Interp *interp) {
113 if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
114 return TCL_ERROR;
115 }
116 printf("creating foo command");
117 Tcl_CreateObjCommand(interp, "foo", fooCmd, NULL, NULL);
118 return TCL_OK;
119 }
120
121 When built into a shared/dynamic library with a suitable name (e.g.
122 foo.dll on Windows, libfoo.so on Solaris and Linux) it can then be
123 loaded into Tcl with the following:
124
125 # Load the extension
126 switch $tcl_platform(platform) {
127 windows {
128 load ./foo.dll
129 }
130 unix {
131 load ./libfoo[info sharedlibextension]
132 }
133 }
134
135 # Now execute the command defined by the extension
136 foo
137
138
140 info sharedlibextension, Tcl_StaticPackage(3), safe(n)
141
142
144 binary code, loading, safe interpreter, shared library
145
146
147
148Tcl 7.5 load(n)