4e9829e254ef5821e1438f436f225eb2baa969eb
[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 platform-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 
80 <filename>/lib/libmylibrary.so</filename>. On a Windows system, using 
81 <filename>\Windows</filename> as the directory it will return
82 <filename>\Windows\mylibrary.dll</filename>.
83 </para>
84
85 @directory: the directory where the module is. This can be %NULL or the empty
86 string to indicate that the standard platform-specific directories will be 
87 used, though that is not recommended.
88 @module_name: the name of the module.
89 @Returns: the complete path of the module, including the standard library
90 prefix and suffix. This should be freed when no longer needed.
91
92
93 <!-- ##### FUNCTION g_module_open ##### -->
94 <para>
95 Opens a module. If the module has already been opened, its reference
96 count is incremented. 
97 </para>
98
99 <para>
100 First of all g_module_open() tries to open @file_name as a module. If
101 that fails and @file_name has the ".la"-suffix (and is a libtool archive) 
102 it tries to open the corresponding module. If that fails and it doesn't 
103 have the proper module suffix for the platform (#G_MODULE_SUFFIX), this 
104 suffix will be appended and the corresponding module will be opended. If 
105 that fails and @file_name doesn't have the ".la"-suffix, this suffix is 
106 appended and g_module_open() tries to open the corresponding module. If 
107 eventually that fails as well, %NULL is returned.
108 </para>
109
110 @file_name: the name of the file containing the module.
111 @flags: the flags used for opening the module. This can be the logical
112 OR of any of the #GModuleFlags.
113 @Returns: a #GModule on success, or %NULL on failure.
114
115
116 <!-- ##### ENUM GModuleFlags ##### -->
117 <para>
118 Flags passed to g_module_open(). Note that these flags are
119 not supported on all platforms.
120 </para>
121
122 @G_MODULE_BIND_LAZY: specifies that symbols are only resolved when needed.
123   The default action is to bind all symbols when the module is loaded.
124 @G_MODULE_BIND_LOCAL:specifies that symbols in the module should
125   not be added to the global name space.  The default action on most
126   platforms is to place symbols in the module in the global name space,
127   which may cause conflicts with existing symbols. 
128 @G_MODULE_BIND_MASK: mask for all flags.
129
130 <!-- ##### FUNCTION g_module_symbol ##### -->
131 <para>
132 Gets a symbol pointer from a module.
133 </para>
134
135 @module: a #GModule.
136 @symbol_name: the name of the symbol to find.
137 @symbol: returns the pointer to the symbol value.
138 @Returns: %TRUE on success.
139
140
141 <!-- ##### FUNCTION g_module_name ##### -->
142 <para>
143 Gets the filename from a #GModule.
144 </para>
145
146 @module: a #GModule.
147 @Returns: the filename of the module, or "main" if the module is the main
148 program itself.
149
150
151 <!-- ##### FUNCTION g_module_make_resident ##### -->
152 <para>
153 Ensures that a module will never be unloaded.
154 Any future g_module_close() calls on the module will be ignored.
155 </para>
156
157 @module: a #GModule to make permanently resident.
158
159
160 <!-- ##### FUNCTION g_module_close ##### -->
161 <para>
162 Closes a module.
163 </para>
164
165 @module: a #GModule to close.
166 @Returns: %TRUE on success.
167
168
169 <!-- ##### FUNCTION g_module_error ##### -->
170 <para>
171 Gets a string describing the last module error.
172 </para>
173
174 @Returns: a string describing the last module error.
175
176
177 <!-- ##### USER_FUNCTION GModuleCheckInit ##### -->
178 <para>
179 Specifies the type of the module initialization function.
180 If a module contains a function named g_module_check_init() it is called
181 automatically when the module is loaded. It is passed the #GModule structure
182 and should return %NULL on success or a string describing the initialization
183 error.
184 </para>
185
186 @module: the #GModule corresponding to the module which has just been loaded.
187 @Returns: %NULL on success, or a string describing the initialization error.
188
189
190 <!-- ##### USER_FUNCTION GModuleUnload ##### -->
191 <para>
192 Specifies the type of the module function called when it is unloaded.
193 If a module contains a function named g_module_unload() it is called
194 automatically when the module is unloaded.
195 It is passed the #GModule structure.
196 </para>
197
198 @module: the #GModule about to be unloaded.
199
200
201 <!-- ##### MACRO G_MODULE_SUFFIX ##### -->
202 <para>
203 Expands to the proper shared library suffix for the current platform
204 without the leading dot. For the most Unices and Linux this is "so",
205 for some HP-UX versions this is "sl" and for Windows this is "dll".
206 </para>
207
208
209
210 <!-- ##### MACRO G_MODULE_EXPORT ##### -->
211 <para>
212 Used to declare functions exported by modules.
213 </para>
214
215
216
217 <!-- ##### MACRO G_MODULE_IMPORT ##### -->
218 <para>
219 Used to declare functions imported from modules.
220 </para>
221
222
223