Shared Libraries

The easiest way to use a shared library is to ignore the fact that it is a shared library. The C compiler automatically uses shared libraries instead of static ones unless it is explicitly told to link with static libraries. However, there are three other ways to use shared libraries. One, explicitly loading and unloading them from within a program while the program runs, is called dynamic loading.

When you run a program, the dynamic loader usually looks in a cache (the /etc/old.so.cache file, created by running the ldconfig command) of libraries that are in directories listed in the /etc/ld.so.conf file to find libraries that the program needs. You can determine the libraries a program requires by running:

ldd program_name

and this will return a list of shared libraries the program requires and whether or not they were located. However, if the LD_LIBRARY_PATH environment variable is set, it will first dynamically scan the directories mentioned in LD_LIBRARY_PATH (a colon separted list of directories, similar to the PATH variable) and load all directories it finds in the path, before it looks in its cache.

    • 3rd party libraries that should be globally accessible should be placed in the /etc/ld.so.conf file.

    • Run ldconfig anytime you make changes to /etc/ld.so.conf.d.

    • A better organization of your shared library entries would be to create a file under the directory /etc/ld.so.conf.d, named as the 3rd party product you installed. In that file place the 3rd party library paths.

    • Only use the shell variable LD_LIBRARY_PATH when you wish to override the settings in /etc/ld.so.conf, or, when you install a new library that is private for that user account.

    • The LD_LIBRARY_PATH should only be used for situations where a user account needs to include a private library.