1load(n)                      Tcl Built-In Commands                     load(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       load - Load machine code and initialize new commands
9

SYNOPSIS

11       load fileName
12       load fileName packageName
13       load fileName packageName interp
14_________________________________________________________________
15
16

DESCRIPTION

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.  For Tcl versions lower than │
61       8.5, it is not possible to unload or reload a package. From version 8.5 │
62       however,  the  unload  command allows the unloading of libraries loaded │
63       with load, for libraries that are aware of the Tcl's  unloading  mecha‐ │
64       nism.
65
66       The load command also supports packages that are statically linked with
67       the application, if those packages have been registered by calling  the
68       Tcl_StaticPackage  procedure.   If  fileName  is  an empty string, then
69       packageName must be specified.
70
71       If packageName is omitted or specified as an empty string, Tcl tries to
72       guess the name of the package.  This may be done differently on differ‐
73       ent platforms.  The default guess, which is used  on  most  UNIX  plat‐
74       forms,  is  to  take  the last element of fileName, strip off the first
75       three characters if they are lib, and use any following alphabetic  and
76       underline characters as the module name.  For example, the command load
77       libxyz4.2.so uses the module name xyz and the command load  bin/last.so
78       {} uses the module name last.
79
80       If  fileName  is  an  empty string, then packageName must be specified.
81       The load command first searches for a statically  loaded  package  (one
82       that has been registered by calling the Tcl_StaticPackage procedure) by
83       that name; if one is found, it is used.  Otherwise,  the  load  command
84       searches  for a dynamically loaded package by that name, and uses it if
85       it is found.  If several different files have been loaded with  differ‐
86       ent versions of the package, Tcl picks the file that was loaded first.
87

PORTABILITY ISSUES

89       Windows
90              When  a  load  fails  with “library not found” error, it is also
91              possible that a dependent library was not  found.   To  see  the
92              dependent  libraries, type “dumpbin -imports <dllname>” in a DOS
93              console to see what the library must import.  When loading a DLL
94              in  the  current  directory,  Windows will ignore “./” as a path
95              specifier and use a search heuristic to find  the  DLL  instead.
96              To avoid this, load the DLL with:
97              load [file join [pwd] mylib.DLL]
98

BUGS

100       If  the  same  file is loaded by different fileNames, it will be loaded
101       into the process's address space multiple times.  The behavior of  this
102       varies  from  system  to  system (some systems may detect the redundant
103       loads, others may not).
104

EXAMPLE

106       The following is a minimal extension:
107
108              #include <tcl.h>
109              #include <stdio.h>
110              static int fooCmd(ClientData clientData,
111                      Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
112                  printf("called with %d arguments\n", objc);
113                  return TCL_OK;
114              }
115              int Foo_Init(Tcl_Interp *interp) {
116                  if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
117                return TCL_ERROR;
118                  }
119                  printf("creating foo command");
120                  Tcl_CreateObjCommand(interp, "foo", fooCmd, NULL, NULL);
121                  return TCL_OK;
122              }
123
124       When built into a shared/dynamic library with  a  suitable  name  (e.g.
125       foo.dll  on  Windows,  libfoo.so  on  Solaris and Linux) it can then be
126       loaded into Tcl with the following:
127
128              # Load the extension
129              switch $tcl_platform(platform) {
130                 windows {
131                    load [file join [pwd] foo.dll]
132                 }
133                 unix {
134                    load [file join [pwd] libfoo[info sharedlibextension]]
135                 }
136              }
137
138              # Now execute the command defined by the extension
139              foo
140
141

SEE ALSO

143       info sharedlibextension, Tcl_StaticPackage(3), safe(n)
144
145

KEYWORDS

147       binary code, loading, safe interpreter, shared library
148
149
150
151Tcl                                   7.5                              load(n)
Impressum