0b7ba74dceda762bdbb77dd99ac1e9e9f8d57b76
[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 dlopen() (e.g. Linux/Sun), as well as HP-UX via its
13 shl_load() 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 <para>
45 <example>
46 <title>Calling a function defined in a <structname>GModule</structname></title>
47 <programlisting>
48 /* the function signature for 'say_hello' */
49 typedef void (* SayHelloFunc) (const char *message);
50
51 gboolean
52 just_say_hello (const char *filename, GError **error)
53 {
54   SayHelloFunc  say_hello;
55   GModule      *module;
56
57   module = g_module_open (filename, G_MODULE_BIND_LAZY);
58   if (!module)
59     {
60       g_set_error (error, FOO_ERROR, FOO_ERROR_BLAH,
61                    "&percnt;s", g_module_error (<!-- -->));
62       return FALSE;
63     }
64
65   if (!g_module_symbol (module, "say_hello", (gpointer *)&amp;say_hello))
66     {
67       g_set_error (error, SAY_ERROR, SAY_ERROR_OPEN,
68                    "&percnt;s: &percnt;s", filename, g_module_error (<!-- -->));
69       if (!g_module_close (module))
70         g_warning ("&percnt;s: &percnt;s", filename, g_module_error (<!-- -->));
71       return FALSE;
72     }
73
74   if (say_hello == NULL)
75     {
76       g_set_error (error, SAY_ERROR, SAY_ERROR_OPEN, "symbol say_hello is NULL");
77       if (!g_module_close (module))
78         g_warning ("&percnt;s: &percnt;s", filename, g_module_error (<!-- -->));
79       return FALSE;
80     }
81
82   /* call our function in the module */
83   say_hello ("Hello world!");
84
85   if (!g_module_close (module))
86     g_warning ("&percnt;s: &percnt;s", filename, g_module_error (<!-- -->));
87
88   return TRUE;
89 }
90 </programlisting>
91 </example>
92 </para>
93
94 <!-- ##### SECTION See_Also ##### -->
95 <para>
96
97 </para>
98
99 <!-- ##### SECTION Stability_Level ##### -->
100
101
102 <!-- ##### SECTION Image ##### -->
103
104
105 <!-- ##### STRUCT GModule ##### -->
106 <para>
107 The #GModule struct is an opaque data structure to represent a
108 <link linkend="glib-Dynamic-Loading-of-Modules">Dynamically-Loaded Module</link>.
109 It should only be accessed via the following functions.
110 </para>
111
112
113 <!-- ##### FUNCTION g_module_supported ##### -->
114 <para>
115 Checks if modules are supported on the current platform.
116 </para>
117
118 @void: 
119 @Returns: %TRUE if modules are supported.
120
121
122 <!-- ##### FUNCTION g_module_build_path ##### -->
123 <para>
124 A portable way to build the filename of a module. The platform-specific
125 prefix and suffix are added to the filename, if needed, and the result is
126 added to the directory, using the correct separator character.
127 </para>
128 <para>
129 The directory should specify the directory where the module can be found.
130 It can be %NULL or an empty string to indicate that the module is in a standard
131 platform-specific directory, though this is not recommended since the
132 wrong module may be found.
133 </para>
134 <para>
135 For example, calling g_module_build_path() on a Linux system with a @directory
136 of <filename>/lib</filename> and a @module_name of "mylibrary" will return 
137 <filename>/lib/libmylibrary.so</filename>. On a Windows system, using 
138 <filename>\Windows</filename> as the directory it will return
139 <filename>\Windows\mylibrary.dll</filename>.
140 </para>
141
142 @directory: the directory where the module is. This can be %NULL or the empty
143 string to indicate that the standard platform-specific directories will be 
144 used, though that is not recommended.
145 @module_name: the name of the module.
146 @Returns: the complete path of the module, including the standard library
147 prefix and suffix. This should be freed when no longer needed.
148
149
150 <!-- ##### MACRO g_module_open ##### -->
151 <para>
152 Opens a module. If the module has already been opened, its reference
153 count is incremented. 
154 </para>
155
156 <para>
157 First of all g_module_open() tries to open @file_name as a module. If
158 that fails and @file_name has the ".la"-suffix (and is a libtool archive) 
159 it tries to open the corresponding module. If that fails and it doesn't 
160 have the proper module suffix for the platform (#G_MODULE_SUFFIX), this 
161 suffix will be appended and the corresponding module will be opended. If 
162 that fails and @file_name doesn't have the ".la"-suffix, this suffix is 
163 appended and g_module_open() tries to open the corresponding module. If 
164 eventually that fails as well, %NULL is returned.
165 </para>
166
167 @Returns: a #GModule on success, or %NULL on failure.
168 <!-- # Unused Parameters # -->
169 @file_name: the name of the file containing the module, or %NULL to obtain
170   a #GModule representing the main program itself.
171 @flags: the flags used for opening the module. This can be the logical
172 OR of any of the #GModuleFlags.
173
174
175 <!-- ##### ENUM GModuleFlags ##### -->
176 <para>
177 Flags passed to g_module_open(). Note that these flags are
178 not supported on all platforms.
179 </para>
180
181 @G_MODULE_BIND_LAZY: specifies that symbols are only resolved when needed.
182   The default action is to bind all symbols when the module is loaded.
183 @G_MODULE_BIND_LOCAL: specifies that symbols in the module should
184   not be added to the global name space.  The default action on most
185   platforms is to place symbols in the module in the global name space,
186   which may cause conflicts with existing symbols.
187 @G_MODULE_BIND_MASK: mask for all flags.
188
189 <!-- ##### FUNCTION g_module_symbol ##### -->
190 <para>
191 Gets a symbol pointer from a module, such as one exported by #G_MODULE_EXPORT.
192 </para>
193 <para>
194 Note that a valid symbol can be %NULL.
195 </para>
196
197 @module: a #GModule.
198 @symbol_name: the name of the symbol to find.
199 @symbol: returns the pointer to the symbol value.
200 @Returns: %TRUE on success.
201
202
203 <!-- ##### MACRO g_module_name ##### -->
204 <para>
205 Gets the filename from a #GModule.
206 </para>
207
208 @Returns: the filename of the module, or "main" if the module is the main
209 program itself.
210 <!-- # Unused Parameters # -->
211 @module: a #GModule.
212
213
214 <!-- ##### FUNCTION g_module_make_resident ##### -->
215 <para>
216 Ensures that a module will never be unloaded.
217 Any future g_module_close() calls on the module will be ignored.
218 </para>
219
220 @module: a #GModule to make permanently resident.
221
222
223 <!-- ##### FUNCTION g_module_close ##### -->
224 <para>
225 Closes a module.
226 </para>
227
228 @module: a #GModule to close.
229 @Returns: %TRUE on success.
230
231
232 <!-- ##### FUNCTION g_module_error ##### -->
233 <para>
234 Gets a string describing the last module error.
235 </para>
236
237 @void: 
238 @Returns: a string describing the last module error.
239
240
241 <!-- ##### USER_FUNCTION GModuleCheckInit ##### -->
242 <para>
243 Specifies the type of the module initialization function.
244 <indexterm zone="g-module-check-init"><primary>g_module_check_init</primary></indexterm>
245 If a module contains a function named g_module_check_init() it is called
246 automatically when the module is loaded. It is passed the #GModule structure
247 and should return %NULL on success or a string describing the initialization
248 error.
249 </para>
250
251 @module: the #GModule corresponding to the module which has just been loaded.
252 @Returns: %NULL on success, or a string describing the initialization error.
253
254
255 <!-- ##### USER_FUNCTION GModuleUnload ##### -->
256 <para>
257 <indexterm zone="g-module-unload"><primary>g_module_unload</primary></indexterm>
258 Specifies the type of the module function called when it is unloaded.
259 If a module contains a function named g_module_unload() it is called
260 automatically when the module is unloaded.
261 It is passed the #GModule structure.
262 </para>
263
264 @module: the #GModule about to be unloaded.
265
266
267 <!-- ##### MACRO G_MODULE_SUFFIX ##### -->
268 <para>
269 Expands to the proper shared library suffix for the current platform
270 without the leading dot. For the most Unices and Linux this is "so",
271 for some HP-UX versions this is "sl" and for Windows this is "dll".
272 </para>
273
274
275
276 <!-- ##### MACRO G_MODULE_EXPORT ##### -->
277 <para>
278 Used to declare functions exported by modules. This is a no-op on Linux and
279 Unices, but when compiling for Windows, it marks a symbol to be exported from
280 the library or executable being built.
281 </para>
282
283
284
285 <!-- ##### MACRO G_MODULE_IMPORT ##### -->
286 <para>
287 Used to declare functions imported from modules.
288 </para>
289
290
291