f3012014d63ca33bd00d524683802e1bb3e2bcb8
[platform/upstream/glib.git] / docs / reference / glib / tmpl / modules.sgml
1 <!-- ##### SECTION Title ##### -->
2 Dynamic Loading of Modules
3
4 <!-- ##### SECTION Short_Description ##### -->
5 portable method for dynamically loading 'plug-ins'.
6
7 <!-- ##### SECTION Long_Description ##### -->
8 <para>
9 These functions provide a portable way to dynamically load object files
10 (commonly known as 'plug-ins').
11 The current implementation supports all systems that provide
12 an implementation of <function>dlopen()</function> (e.g. Linux/Sun), as well as HP-UX via its
13 <function>shl_load()</function> mechanism, and Windows platforms via DLLs.
14 </para>
15
16 <para>
17 A program which wants to use these functions must be linked to the
18 libraries output by the command <command>pkg-config --libs gmodule-2.0</command>.
19 </para>
20
21 <para>
22 To use them you must first determine whether dynamic loading
23 is supported on the platform by calling g_module_supported().
24 If it is, you can open a module with g_module_open(),
25 find the module's symbols (e.g. function names) with g_module_symbol(),
26 and later close the module with g_module_close().
27 g_module_name() will return the file name of a currently opened module.
28 </para>
29 <para>
30 If any of the above functions fail, the error status can be found with
31 g_module_error().
32 </para>
33 <para>
34 The gmodule implementation features reference counting for opened modules,
35 and supports hook functions within a module which are called when the
36 module is loaded and unloaded (see #GModuleCheckInit and #GModuleUnload).
37 </para>
38 <para>
39 If your module introduces static data to common subsystems in the running
40 program, e.g. through calling <literal>g_quark_from_static_string ("my-module-stuff")</literal>,
41 it must ensure that it is never unloaded, by calling g_module_make_resident().
42 </para>
43
44 <!-- ##### SECTION See_Also ##### -->
45 <para>
46
47 </para>
48
49 <!-- ##### STRUCT GModule ##### -->
50 <para>
51 The #GModule struct is an opaque data structure to represent a
52 <link linkend="glib-Dynamic-Loading-of-Modules">Dynamically-Loaded Module</link>.
53 It should only be accessed via the following functions.
54 </para>
55
56
57 <!-- ##### FUNCTION g_module_supported ##### -->
58 <para>
59 Checks if modules are supported on the current platform.
60 </para>
61
62 @Returns: %TRUE if modules are supported.
63
64
65 <!-- ##### FUNCTION g_module_build_path ##### -->
66 <para>
67 A portable way to build the filename of a module. The platform-specific
68 prefix and suffix are added to the filename, if needed, and the result is
69 added to the directory, using the correct separator character.
70 </para>
71 <para>
72 The directory should specify the directory where the module can be found.
73 It can be %NULL or an empty string to indicate that the module is in a standard
74 operating-system specific directory, though this is not recommended since the
75 wrong module may be found.
76 </para>
77 <para>
78 For example, calling g_module_build_path() on a Linux system with a directory
79 of <filename>/lib</filename> and a module_name of "mylibrary" will return <filename>/lib/libmylibrary.so</filename>.
80 On a Windows system, using <filename>\Windows</filename> as the directory it will return
81 <filename>\Windows\mylibrary.dll</filename>.
82 </para>
83
84 @directory: the directory where the module is. This can be %NULL or the empty
85 string to indicate that the standard operating system-specific directories
86 will be used, though that is not recommended.
87 @module_name: the name of the module.
88 @Returns: the complete path of the module, including the standard library
89 prefix and suffix. This should be freed when no longer needed.
90
91
92 <!-- ##### FUNCTION g_module_open ##### -->
93 <para>
94 Opens a module. If the module has already been opened, its reference
95 count is incremented. 
96 </para>
97
98 <para>
99 First of all g_module_open() tries to open @file_name as a module. If
100 that fails and @file_name has the ".la"-suffix (and is a libtool archive) 
101 it tries to open the corresponding module. If that fails and it doesn't 
102 have the proper module suffix for that platform (#G_MODULE_SUFFIX), this 
103 suffix will be appended and the corresponding module will be opended. If 
104 that fails and @file_name doesn't have the ".la"-suffix, this suffix is 
105 appended and g_module_open() tries to open the corresponding module. If 
106 eventually that fails as well, %NULL is returned.
107 </para>
108
109 @file_name: the name of the file containing the module.
110 @flags: the flags used for opening the module. Currently this can be 0 or
111 #G_MODULE_BIND_LAZY for lazy binding, where symbols are only bound when needed.
112 @Returns: a #GModule on success, or %NULL on failure.
113
114
115 <!-- ##### ENUM GModuleFlags ##### -->
116 <para>
117 Flags passed to g_module_open().
118 #G_MODULE_BIND_LAZY specifies that symbols are only resolved when needed.
119 The default action is to bind all symbols when the module is loaded.
120 (#G_MODULE_BIND_LAZY is not supported on all platforms.)
121 </para>
122
123 @G_MODULE_BIND_LAZY: 
124 @G_MODULE_BIND_MASK: 
125
126 <!-- ##### FUNCTION g_module_symbol ##### -->
127 <para>
128 Gets a symbol pointer from a module.
129 </para>
130
131 @module: the module.
132 @symbol_name: the name of the symbol to find.
133 @symbol: returns the pointer to the symbol value.
134 @Returns: %TRUE on success.
135
136
137 <!-- ##### FUNCTION g_module_name ##### -->
138 <para>
139 Gets the file name from a #GModule.
140 </para>
141
142 @module: the module.
143 @Returns: the file name of the module, or "main" if the module is the main
144 program itself.
145
146
147 <!-- ##### FUNCTION g_module_make_resident ##### -->
148 <para>
149 Ensures that a module will never be unloaded.
150 Any future g_module_close() calls on the module will be ignored.
151 </para>
152
153 @module: a module to make permanently resident.
154
155
156 <!-- ##### FUNCTION g_module_close ##### -->
157 <para>
158 Closes a module.
159 </para>
160
161 @module: the module to close.
162 @Returns: %TRUE on success.
163
164
165 <!-- ##### FUNCTION g_module_error ##### -->
166 <para>
167 Gets a string describing the last module error.
168 </para>
169
170 @Returns: a string describing the last module error.
171
172
173 <!-- ##### USER_FUNCTION GModuleCheckInit ##### -->
174 <para>
175 Specifies the type of the module initialization function.
176 If a module contains a function named g_module_check_init() it is called
177 automatically when the module is loaded. It is passed the #GModule structure
178 and should return %NULL on success or a string describing the initialization
179 error.
180 </para>
181
182 @module: the #GModule corresponding to the module which has just been loaded.
183 @Returns: %NULL on success, or a string describing the initialization error.
184
185
186 <!-- ##### USER_FUNCTION GModuleUnload ##### -->
187 <para>
188 Specifies the type of the module function called when it is unloaded.
189 If a module contains a function named g_module_unload() it is called
190 automatically when the module is unloaded.
191 It is passed the #GModule structure.
192 </para>
193
194 @module: the module about to be unloaded.
195
196
197 <!-- ##### MACRO G_MODULE_SUFFIX ##### -->
198 <para>
199 Expands to the proper shared library suffix for the current platform
200 without the leading dot. For the most Unices and Linux this is "so",
201 for some HPUX versions this is "sl" and for Windows this is "dll".
202 </para>
203
204
205
206 <!-- ##### MACRO G_MODULE_EXPORT ##### -->
207 <para>
208 Used to declare functions exported by modules.
209 </para>
210
211
212
213 <!-- ##### MACRO G_MODULE_IMPORT ##### -->
214 <para>
215 Used to declare functions imported from modules.
216 </para>
217
218
219