ffd70ba8a5d4c1566508a02177b0ec007f5141f1
[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().
119 #G_MODULE_BIND_LAZY specifies that symbols are only resolved when needed.
120 The default action is to bind all symbols when the module is loaded.
121 (#G_MODULE_BIND_LAZY is not supported on all platforms.)
122 #G_MODULE_BIND_LOCAL specifies that symbols in the module should
123 not be added to the global name space.  The default action on most
124 platforms is to place symbols in the module in the global name space,
125 which may cause conflicts with existing symbols.
126 (#G_MODULE_BIND_LOCAL is not supported on all platforms.)
127 </para>
128
129 @G_MODULE_BIND_LAZY: 
130 @G_MODULE_BIND_LOCAL: 
131 @G_MODULE_BIND_MASK: 
132
133 <!-- ##### FUNCTION g_module_symbol ##### -->
134 <para>
135 Gets a symbol pointer from a module.
136 </para>
137
138 @module: a #GModule.
139 @symbol_name: the name of the symbol to find.
140 @symbol: returns the pointer to the symbol value.
141 @Returns: %TRUE on success.
142
143
144 <!-- ##### FUNCTION g_module_name ##### -->
145 <para>
146 Gets the filename from a #GModule.
147 </para>
148
149 @module: a #GModule.
150 @Returns: the filename of the module, or "main" if the module is the main
151 program itself.
152
153
154 <!-- ##### FUNCTION g_module_make_resident ##### -->
155 <para>
156 Ensures that a module will never be unloaded.
157 Any future g_module_close() calls on the module will be ignored.
158 </para>
159
160 @module: a #GModule to make permanently resident.
161
162
163 <!-- ##### FUNCTION g_module_close ##### -->
164 <para>
165 Closes a module.
166 </para>
167
168 @module: a #GModule to close.
169 @Returns: %TRUE on success.
170
171
172 <!-- ##### FUNCTION g_module_error ##### -->
173 <para>
174 Gets a string describing the last module error.
175 </para>
176
177 @Returns: a string describing the last module error.
178
179
180 <!-- ##### USER_FUNCTION GModuleCheckInit ##### -->
181 <para>
182 Specifies the type of the module initialization function.
183 If a module contains a function named g_module_check_init() it is called
184 automatically when the module is loaded. It is passed the #GModule structure
185 and should return %NULL on success or a string describing the initialization
186 error.
187 </para>
188
189 @module: the #GModule corresponding to the module which has just been loaded.
190 @Returns: %NULL on success, or a string describing the initialization error.
191
192
193 <!-- ##### USER_FUNCTION GModuleUnload ##### -->
194 <para>
195 Specifies the type of the module function called when it is unloaded.
196 If a module contains a function named g_module_unload() it is called
197 automatically when the module is unloaded.
198 It is passed the #GModule structure.
199 </para>
200
201 @module: the #GModule about to be unloaded.
202
203
204 <!-- ##### MACRO G_MODULE_SUFFIX ##### -->
205 <para>
206 Expands to the proper shared library suffix for the current platform
207 without the leading dot. For the most Unices and Linux this is "so",
208 for some HP-UX versions this is "sl" and for Windows this is "dll".
209 </para>
210
211
212
213 <!-- ##### MACRO G_MODULE_EXPORT ##### -->
214 <para>
215 Used to declare functions exported by modules.
216 </para>
217
218
219
220 <!-- ##### MACRO G_MODULE_IMPORT ##### -->
221 <para>
222 Used to declare functions imported from modules.
223 </para>
224
225
226