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