C++ User's Guide

Using Shared Libraries

The following shared libraries are included with the C++ compiler:

The occurrence of each shared object linked with the program is recorded in the resulting executable (a.out file); this information is used by ld.so to perform dynamic link editing at runtime. Because the work of incorporating the library code into an address space is deferred, the runtime behavior of the program using a shared library is sensitive to an environment change--that is, moving a library from one directory to another. For example, if your program is linked with libcomplex.so.5 in /opt/SUNWspro/SC5.0/lib in the Solaris 2.6 environment, and the libcomplex.so.5 library is later moved into /opt2/SUNWspro/SC5.0/lib, the following message is displayed when you run the binary code:


ld.so: libcomplex.so.5: not found

You can still run the old binary code without recompiling it by setting the environment variable LD_LIBRARY_PATH to the new library directory.

In a C shell:


demo% setenv LD_LIBRARY_PATH \
/opt2/SUNWspro/SC5.0/lib:${LD_LIBRARY_PATH}

In a Bourne shell:


demo$ LD_LIBRARY_PATH=/opt2/SUNWspro/SC5.0/lib:${LD_LIBRARY_PATH}
demo$ export LD_LIBRARY_PATH

The LD_LIBRARY_PATH has a list of directories, usually separated by colons. When you run a C++ program, the dynamic loader searches the directories in LD_LIBRARY_PATH before the default directories.

Use the ldd command as shown in the following example to see which libraries are linked dynamically in your executable:

% ldd a.out

This step should rarely be necessary, because the shared libraries are seldom moved.


Note -

When shared libraries are opened with dlopen, RTLD_GLOBAL must be used for exceptions to work.


See Linker and Libraries Guide for more information on using shared libraries.