1 <!-- ##### SECTION Title ##### -->
2 Dynamic Loading of Modules
4 <!-- ##### SECTION Short_Description ##### -->
5 portable method for dynamically loading 'plug-ins'.
7 <!-- ##### SECTION Long_Description ##### -->
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.
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>.
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.
30 If any of the above functions fail, the error status can be found with
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).
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().
44 <!-- ##### SECTION See_Also ##### -->
49 <!-- ##### STRUCT GModule ##### -->
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.
57 <!-- ##### FUNCTION g_module_supported ##### -->
59 Checks if modules are supported on the current platform.
62 @Returns: %TRUE if modules are supported.
65 <!-- ##### FUNCTION g_module_build_path ##### -->
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.
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.
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>.
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.
93 <!-- ##### FUNCTION g_module_open ##### -->
95 Opens a module. If the module has already been opened, its reference
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.
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.
116 <!-- ##### ENUM GModuleFlags ##### -->
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.)
130 @G_MODULE_BIND_LOCAL:
133 <!-- ##### FUNCTION g_module_symbol ##### -->
135 Gets a symbol pointer from a module.
139 @symbol_name: the name of the symbol to find.
140 @symbol: returns the pointer to the symbol value.
141 @Returns: %TRUE on success.
144 <!-- ##### FUNCTION g_module_name ##### -->
146 Gets the filename from a #GModule.
150 @Returns: the filename of the module, or "main" if the module is the main
154 <!-- ##### FUNCTION g_module_make_resident ##### -->
156 Ensures that a module will never be unloaded.
157 Any future g_module_close() calls on the module will be ignored.
160 @module: a #GModule to make permanently resident.
163 <!-- ##### FUNCTION g_module_close ##### -->
168 @module: a #GModule to close.
169 @Returns: %TRUE on success.
172 <!-- ##### FUNCTION g_module_error ##### -->
174 Gets a string describing the last module error.
177 @Returns: a string describing the last module error.
180 <!-- ##### USER_FUNCTION GModuleCheckInit ##### -->
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
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.
193 <!-- ##### USER_FUNCTION GModuleUnload ##### -->
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.
201 @module: the #GModule about to be unloaded.
204 <!-- ##### MACRO G_MODULE_SUFFIX ##### -->
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".
213 <!-- ##### MACRO G_MODULE_EXPORT ##### -->
215 Used to declare functions exported by modules.
220 <!-- ##### MACRO G_MODULE_IMPORT ##### -->
222 Used to declare functions imported from modules.