#include <dlfcn.h>
If an error occurs when calling shared library management routines, the system error variable errno is set to an appropriate error value. Constants are defined for these error values in /usr/include/errno.h (see errno(2)). Thus, if a program checks for these error values, it must include errno.h:
#include <errno.h>
Throughout this section, all examples are given in C. To learn how to call these routines from aC++ or Fortran, see the
Using Shared Libraries with cc and ld Options
In
Initializers for Shared Libraries
A shared library can have an initialization routine - known as an initializer - that is called when the load module (a shared library or executable) is loaded (initializer) or explicitly unloaded (finalizer or terminator). Typically, an initializer is used to initialize a shared library's data when the library is loaded. When a program begins execution, its initializers are called before any other user code is executed. This allows for setup at initialization and cleanup at termination. Also, when a shared library is explicitly loaded using shl_load or dlopen or unloaded using shl_unload or dlclose, its initializers and terminators are called at the appropriate time. In
Styles of Initializers
The linker supports two different types of initializers and terminators:
•Init/fini style. (Not supported in
•
Init/Fini Style Initializers
This style uses init and fini functions to handle initialization and finalization (terminator) operations. Init Initializers (inits) are called before the user's code starts or when a shared library is loaded. Functions specified with this option must not take arguments and return nothing (void functions). The C compiler pragma "init" can be used to declare these functions. For example:
#pragma init "my_init"
void my_init() { ... do some initializations ... }
The ld command also supports the +init function option to specify the initializer. Use this option while building a shared library, an incomplete executable, or fully bound executable. Use the +init option to specify the initializer functions, to be invoked in reverse order, the order the functions appear right to left on the command line. Initializers are called in
138 Shared Library Management Routines