244 Chapter 6
Shared Library Management Routines
The dlopen Shared Library Management Routines
The symbols introduced by dlopen operations and available through
dlsym are those which are “exported” as symbols of global scope by the
shared library. For shared libraries, such symbols are typically those that
were specified in (for example) C source code as having extern linkage.
For a.out files, only a subset of externally visible symbols are typically
exported: specifically those referenced by the shared libraries with which
the a.out is linked. The exact set of exported symbols for any shared
library or the a.out can be controlled using the linker [see ld(1)].
NOTE The environment variable LD_LIBRARY_PATH should contain a
colon-separated list of directories, in the same format as the PATH
variable [see sh(1)]. LD_LIBRARY_PATH is ignored if the process’ real
user id is different from its effective user id or its real group id is
different from its effective group id [see exec(2)] or if the process has
acquired any privileges [see tfadmin(1M)].
Example
The following example shows how to use dlopen to load a shared library.
The RTLD_GLOBAL flag enables global visibility to symbols in lib1.sl.
The RTLD_LAZY flag indicates that only references to data symbols are to
be relocated and all function symbol references are to be delayed until
their first invocation.
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char **argv)
{
void* handle;
handle = dlopen(“./lib1.sl”, RTLD_GLOBAL | RTLD_LAZY);
if (handle == NULL) {
printf(“Cannot load library\n”);
}
}
The dlerror Routine
Gets diagnostic information.
Syntax
char *dlerror(void);