Library – Usually a system library is a collection of program functions. From programming basics, we can say functions are self-contained code modules or code blocks that perform a certain task within an application. Reason to use function is to use the same function in multiple other files and programs. Linux supports static libraries which are copied into an application when it is compiled. The other are called shared libraries where functions are copied into memory and called when the program is launched (so called loading a library).
Library cache – Catalog of library directories and all libraries contained within those directories. System reads this cache to find needed libraries when loading programs. It finds and loads libraries quickly rather than searching them through the whole system. When new library is added to the system, this library cache file must be updated. To update it, use ldconfig command. When we pull packages from repositories, this command is run automatically and updates all downloaded libraries to its library cache. To see what library files are cached, use ldconfig -v command. The library data is stored in /etc/ld.so.cache file. If you create new library, make new file in /etc/ld.so.conf.d/ directory which contains path to your new library file. Then, run ldconfig –v to update cache and it should work.
A shared library file has following filename format:
The “so” stands for shared library, and for static libraries there would be “a” character. When a program is using shared function, system searches for function’s library file. Locations where system looks for that library file are as follows:
- Program’s path environment variable
- /etc/ld.so.conf.d/ directory
- /etc/ld.so.conf file
- /lib*/ directory
- /usr/lib*/ directory
When the program starts, dynamic linker or dynamic loader searches program’s needed library functions. Once the library is found, dynamic linker copies them into memory and connect them with the program. Dynamic linker is executable with name ld.so or ld-linux.so
If library is missing, use ldd utility. Here are all libraries for htop program:
[aldin@arch ~]$ ldd /usr/bin/htop linux-vdso.so.1 (0x00007ffc7e1eb000) libncursesw.so.6 => /usr/lib/libncursesw.so.6 (0x00007f4c647f4000) libm.so.6 => /usr/lib/libm.so.6 (0x00007f4c646ae000) libnl-3.so.200 => /usr/lib/libnl-3.so.200 (0x00007f4c6468a000) libnl-genl-3.so.200 => /usr/lib/libnl-genl-3.so.200 (0x00007f4c64681000) libc.so.6 => /usr/lib/libc.so.6 (0x00007f4c644b8000) /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007f4c648dc000) libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f4c64496000)