218 Chapter 6
Shared Library Management Routines
The shl_load Shared Library Management Routines
However, programs are not restricted to using shared libraries only in
that situation. For example, rather than linking with any required
libraries, a program could explicitly load libraries as they are needed.
One possible reason for doing this is to minimize virtual memory
overhead. To keep virtual memory resource usage to a minimum, a
program could load libraries with shl_load and unload with
shl_unload when the library is no longer needed. However, it is
normally not necessary to incur the programming overhead of loading
and unloading libraries yourself for the sole reason of managing system
resources.
Note that if shared library initializers have been declared for an
explicitly loaded library, they are called after the library is loaded. For
details, see “Initializers for Shared Libraries”.
To explicitly load a shared library, use the shl_load routine. If loading a
C++ library, use the cxxshl_load routine. This ensures that
constructors of nonlocal static objects are executed when the library is
loaded. The syntax of cxxshl_load is the same as that of shl_load.
In 64-bit mode, shl_load lets you load a compatibility or standard mode
shared libraries. The BIND_BREADTH_FIRST flag overrides the default
depth-first loading mechanism.
shl_load Usage
Since the library was not specified at link time, the program must get the
library name at run time. Here are some practical ways to do this:
• Hard-code the library name into the program (the easiest method).
• Get the library name from an environment variable using the getenv
library routine (see getenv(3C)).
• Get the library path name from the command line through argv.
• Read the library name from a configuration file.
• Prompt for the library path name at run time.
If successful, shl_load returns a shared library handle (of type shl_t),
which uniquely identifies the library. This handle can then be passed to
the shl_findsym or shl_unload routine.