Flevo CFD

Linux Shared Libraries

Libraries are ready-made codes that programs utilize, eliminating the need for programmers to implement them.

In Linux, these files are called Shared Libraries or Shared Objects with the extension so (in Windows, the extension is dll).

Program developers have two ways to use their desired libraries

Embed them directly in their code, known as Static Linking.

Request them from the operating system, known as Dynamic Linking. Due to the increase in program size and other issues, Dynamic Linking is usually preferred. In this post we’re looking into them.

To determine the libraries required by a binary executable file, the ldd command is used

1
ldd /bin/ls

The system looks for library paths in the /etc/ld.so.conf/ file. To expedite library loading, a list of found libraries is stored in the /etc/ld.so.cache/ file, eliminating the need to search all defined paths each time.

To add your desired libraries to the operating system, provide the path containing your libraries to ldconfig

1
ldconfig /path/to/so/files

If you run ldconfig again without arguments, it searches the paths defined in /etc/ld.so.conf/, updates the cache, and your previous command becomes ineffective.

To permanently register your libraries in the system, add the path addresses to the /etc/ld.so.conf/ file and then run ldconfig:

1
2
echo /path/to/so/files >> /etc/ld.so.conf
ldconfig

Additionally, if you want ldconfig to search a path without updating the /etc/ld.so.cache/ file, use the -n switch. To see a list of all found libraries, use the -v switch

1
ldconfig -v

If you lack necessary permissions or have other reasons not to change the system’s global libraries, you can add the library path to the LD_LIBRARY_PATH environment variable

1
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/path/to/so/files/"