Linux dynamic library (.so) search path

  
 

As we all know, the default search paths for Linux dynamic libraries are /lib and /usr/lib. After the dynamic library is created, it is usually copied to these two directories. When a program needs to execute a dynamic library, and the dynamic library has not been loaded into the memory, the system will automatically find the corresponding dynamic library file in the two default search paths, and then load the file into the memory, so that the program You can use the functions in the dynamic library, as well as other resources of the dynamic library. In Linux, the search path of the dynamic library can be specified in the following three ways in addition to the default search path.
Method 1: Specify the dynamic library search path in the configuration file /etc/ld.so.conf.
You can specify the search path for the dynamic library by editing the configuration file /etc/ld.so.conf, which acts as a dynamic library search path. After editing the file, you must run the command ldconfig to make the modified configuration take effect. We illustrate this method by Example 1.
Example 1:
We use the following command to create the dynamic library libpos.so with the source program pos_conf.c (see program 1). For detailed creation process, please refer to [1].
# gcc -c pos_conf.c # gcc -shared -fPCI -o libpos.so pos_conf.o #
#include <stdio.h> void pos() { printf("/root/test/Conf/lib\ "); } Program 1: pos_conf.c
Then compile main.c (see program 2) with the following command to generate the target program pos.
# gcc -o pos main.c -L. -lpos #
void pos(); int main() { pos(); return 0; } Program 2: main.c
Then put the library The file is moved to the directory /root/test/conf/lib.
# mkdir -p /root/test/conf/lib # mv libpos.so /root/test/conf/lib #
Last edit the configuration file /etc/ld.so.conf and append it to this file One line "/root/test/conf/lib".
Run the program pos try.
# ./pos ./pos: error while loading shared libraries: libpos.so: cannot open shared object file: No such file or directory #
An error occurred, the system did not find the dynamic library libpos.so. Looking for the reason, after editing the configuration file /etc/ld.so.conf, the command ldconfig was not run, so the modification has not yet taken effect. Let's try it after running ldconfig.
# ldconfig # ./pos /root/test/conf/lib #
The program pos runs successfully and prints the correct result.
Method 2: Specify the dynamic library search path through the environment variable LD_LIBRARY_PATH.
The dynamic library search path can also be specified by setting the environment variable LD_LIBRARY_PATH. When multiple dynamic library search paths are specified through this environment variable, the paths are separated by a colon ":". The method will be described below by way of Example 2.
Example 2:
We use the following command to create the dynamic library libpos.so with the source program pos_env.c (see program 3).
# gcc -c pos_env.c # gcc -shared -fPCI -o libpos.so pos_env.o #
#include <stdio.h> void pos() { printf("/root/test/Env/lib\ "); } Program 3: pos_env.c
The executable file pos for testing can use the target program pos obtained in Example 1, and does not need to be compiled again. Because the function pos in pos_conf.c and the function pos function prototype in pos_env.c are the same, and the dynamic library name is the same, this is like re-creating the library after modifying the dynamic library pos. This is one of the advantages of using a dynamic library.
Then move the dynamic library libpos.so to the directory /root/test/conf/lib.
# mkdir -p /root/test/env/lib # mv libpos.so /root/test/env/lib #
We can use export to set the environment variable, after setting the environment variable This environment variable is valid in the command.
Example:
# export LD_LIBRARY_PATH=/root/test/env/lib #
But for the convenience of this article, another way to set environment variables is to add environment variables before the command. The environment variable is only valid for this command. When the command is executed, the environment variable is invalid. Such as the following command:
# LD_LIBRARY_PATH=/root/test/env/lib ./pos /root/test/env/lib #
Program pos runs successfully, and the result of printing is "/root/test/Env/lib", is the result of the function pos in the program pos_env.c. So the dynamic library searched by the program pos is /root/test/env/lib/libpos.so.

Copyright © Windows knowledge All Rights Reserved