Debian Codegraph @

doc/load.n

raw
1 '\"
2 '\" Copyright (c) 1995-1996 Sun Microsystems, Inc.
3 '\"
4 '\" See the file "license.terms" for information on usage and redistribution
5 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6 '\"
7 .TH load n 7.5 Tcl "Tcl Built-In Commands"
8 .so man.macros
9 .BS
10 '\" Note: do not modify the .SH NAME line immediately below!
11 .SH NAME
12 load \- Load machine code and initialize new commands
13 .SH SYNOPSIS
14 \fBload\fR ?\fB\-global\fR? ?\fB\-lazy\fR? ?\fB\-\-\fR? \fIfileName\fR
15 .br
16 \fBload\fR ?\fB\-global\fR? ?\fB\-lazy\fR? ?\fB\-\-\fR? \fIfileName prefix\fR
17 .br
18 \fBload\fR ?\fB\-global\fR? ?\fB\-lazy\fR? ?\fB\-\-\fR? \fIfileName prefix interp\fR
19 .BE
20 .SH DESCRIPTION
21 .PP
22 This command loads binary code from a file into the
23 application's address space and calls an initialization procedure
24 in the library to incorporate it into an interpreter. \fIfileName\fR
25 is the name of the file containing the code; its exact form varies
26 from system to system but on most systems it is a shared library,
27 such as a \fB.so\fR file under Solaris or a DLL under Windows.
28 \fIprefix\fR is used to compute the name of an initialization procedure.
29 \fIinterp\fR is the path name of the interpreter into which to load
30 the library (see the \fBinterp\fR manual entry for details);
31 if \fIinterp\fR is omitted, it defaults to the
32 interpreter in which the \fBload\fR command was invoked.
33 .PP
34 Once the file has been loaded into the application's address space,
35 one of two initialization procedures will be invoked in the new code.
36 Typically the initialization procedure will add new commands to a
37 Tcl interpreter.
38 The name of the initialization procedure is determined by
39 \fIprefix\fR and whether or not the target interpreter
40 is a safe one. For normal interpreters the name of the initialization
41 procedure will have the form \fIpfx\fB_Init\fR, where \fIpfx\fR
42 is the same as \fIprefix\fR except that the first letter is
43 converted to upper case and all other letters
44 are converted to lower case. For example, if \fIprefix\fR is
45 \fBfoo\fR or \fBFOo\fR, the initialization procedure's name will
46 be \fBFoo_Init\fR.
47 .PP
48 If the target interpreter is a safe interpreter, then the name
49 of the initialization procedure will be \fIpfx\fB_SafeInit\fR
50 instead of \fIpfx\fB_Init\fR.
51 The \fIpfx\fB_SafeInit\fR function should be written carefully, so that it
52 initializes the safe interpreter only with partial functionality provided
53 by the library that is safe for use by untrusted code. For more information
54 on Safe\-Tcl, see the \fBsafe\fR manual entry.
55 .PP
56 The initialization procedure must match the following prototype:
57 .PP
58 .CS
59 typedef int \fBTcl_PackageInitProc\fR(
60 Tcl_Interp *\fIinterp\fR);
61 .CE
62 .PP
63 The \fIinterp\fR argument identifies the interpreter in which the
64 library is to be loaded. The initialization procedure must return
65 \fBTCL_OK\fR or \fBTCL_ERROR\fR to indicate whether or not it completed
66 successfully; in the event of an error it should set the interpreter's result
67 to point to an error message. The result of the \fBload\fR command
68 will be the result returned by the initialization procedure.
69 .PP
70 The actual loading of a file will only be done once for each \fIfileName\fR
71 in an application. If a given \fIfileName\fR is loaded into multiple
72 interpreters, then the first \fBload\fR will load the code and
73 call the initialization procedure; subsequent \fBload\fRs will
74 call the initialization procedure without loading the code again.
75 For Tcl versions lower than 8.5, it is not possible to unload or reload a
76 library. From version 8.5 however, the \fBunload\fR command allows the unloading
77 of libraries loaded with \fBload\fR, for libraries that are aware of the
78 Tcl's unloading mechanism.
79 .PP
80 The \fBload\fR command also supports libraries that are statically
81 linked with the application, if those libraries have been registered
82 by calling the \fBTcl_StaticPackage\fR procedure.
83 If \fIfileName\fR is an empty string, then \fIprefix\fR must
84 be specified.
85 .PP
86 If \fIprefix\fR is omitted or specified as an empty string,
87 Tcl tries to guess the prefix. This may be done differently on
88 different platforms. The default guess, which is used on most
89 UNIX platforms, is to take the last element of
90 \fIfileName\fR, strip off the first three characters if they
91 are \fBlib\fR, and use any following alphabetic and
92 underline characters, converted to titlecase as the prefix.
93 For example, the command \fBload libxyz4.2.so\fR uses the prefix
94 \fBXyz\fR and the command \fBload bin/last.so {}\fR uses the
95 prefix \fBLast\fR.
96 .PP
97 If \fIfileName\fR is an empty string, then \fIprefix\fR must
98 be specified.
99 The \fBload\fR command first searches for a statically loaded library
100 (one that has been registered by calling the \fBTcl_StaticPackage\fR
101 procedure) by that name; if one is found, it is used.
102 Otherwise, the \fBload\fR command searches for a dynamically loaded
103 library by that name, and uses it if it is found. If several
104 different files have been \fBload\fRed with different versions of
105 the library, Tcl picks the file that was loaded first.
106 .PP
107 If \fB\-global\fR is specified preceding the filename, all symbols
108 found in the shared library are exported for global use by other
109 libraries. The option \fB\-lazy\fR delays the actual loading of
110 symbols until their first actual use. The options may be abbreviated.
111 The option \fB\-\-\fR indicates the end of the options, and should
112 be used if you wish to use a filename which starts with \fB\-\fR
113 and you provide a prefix to the \fBload\fR command.
114 .PP
115 On platforms which do not support the \fB\-global\fR or \fB\-lazy\fR
116 options, the options still exist but have no effect. Note that use
117 of the \fB\-global\fR or \fB\-lazy\fR option may lead to crashes
118 in your application later (in case of symbol conflicts resp. missing
119 symbols), which cannot be detected during the \fBload\fR. So, only
120 use this when you know what you are doing, you will not get a nice
121 error message when something is wrong with the loaded library.
122 .SH "PORTABILITY ISSUES"
123 .TP
124 \fBWindows\fR\0\0\0\0\0
125 .
126 When a load fails with
127 .QW "library not found"
128 error, it is also possible
129 that a dependent library was not found. To see the dependent libraries,
130 type
131 .QW "dumpbin -imports <dllname>"
132 in a DOS console to see what the library must import.
133 When loading a DLL in the current directory, Windows will ignore
134 .QW ./
135 as a path specifier and use a search heuristic to find the DLL instead.
136 To avoid this, load the DLL with:
137 .RS
138 .PP
139 .CS
140 \fBload\fR [file join [pwd] mylib.DLL]
141 .CE
142 .RE
143 .SH BUGS
144 .PP
145 If the same file is \fBload\fRed by different \fIfileName\fRs, it will
146 be loaded into the process's address space multiple times. The
147 behavior of this varies from system to system (some systems may
148 detect the redundant loads, others may not).
149 .SH EXAMPLE
150 .PP
151 The following is a minimal extension:
152 .PP
153 .CS
154 #include <tcl.h>
155 #include <stdio.h>
156 static int fooCmd(void *clientData,
157 Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
158 printf("called with %d arguments\en", objc);
159 return TCL_OK;
160 }
161 int Foo_Init(Tcl_Interp *interp) {
162 if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
163 return TCL_ERROR;
164 }
165 printf("creating foo command");
166 Tcl_CreateObjCommand(interp, "foo", fooCmd, NULL, NULL);
167 return TCL_OK;
168 }
169 .CE
170 .PP
171 When built into a shared/dynamic library with a suitable name
172 (e.g. \fBfoo.dll\fR on Windows, \fBlibfoo.so\fR on Solaris and Linux)
173 it can then be loaded into Tcl with the following:
174 .PP
175 .CS
176 # Load the extension
177 switch $tcl_platform(platform) {
178 windows {
179 \fBload\fR [file join [pwd] foo.dll]
180 }
181 unix {
182 \fBload\fR [file join [pwd] libfoo[info sharedlibextension]]
183 }
184 }
185
186 # Now execute the command defined by the extension
187 foo
188 .CE
189 .SH "SEE ALSO"
190 info sharedlibextension, package(n), Tcl_StaticPackage(3), safe(n)
191 .SH KEYWORDS
192 binary code, dynamic library, load, safe interpreter, shared library
193 '\"Local Variables:
194 '\"mode: nroff
195 '\"End: