Linux dynamic library (. So) Search Path

Source: Internet
Author: User
As we all know, the default search paths for Linux dynamic libraries are/lib and/usr/lib. After a dynamic library is created, it is generally copied to these two directories. When a dynamic library is required for program execution and the dynamic library is not loaded into the memory, the system will automatically search for the corresponding dynamic library files in the two default search paths, then load the file to the memory so that the program can use the functions in the dynamic library and other resources of the dynamic library. In Linux, in addition to the default search path, the search path of a dynamic library can be specified in the following three methods.

Method 1: Specify the dynamic library search path in the configuration file/etc/lD. So. conf.

You can specify the search path for a dynamic library by editing the configuration file/etc/lD. So. conf. After editing the file, you must run the ldconfig command to make the modified configuration take effect. We use Example 1 to describe this method.

Example 1:

Run the following command to use the source program pos_conf.c (see program 1) to create the dynamic library libpos. so. For detailed creation process, see [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/N ");
}
Procedure 1: pos_conf.c

Run the following command to compile main. C (see Program 2) to generate the POS of the target program.

# Gcc-o pos main. C-L.-LPOS
#

Void pos ();
Int main ()
{
POs ();
Return 0;
}
Program 2: Main. c

Then, move the library file to the/root/test/CONF/lib directory.

# Mkdir-P/root/test/CONF/lib
# Mv libpos. So/root/test/CONF/lib
#

Finally, edit the configuration file/etc/lD. So. conf and add a line "/root/test/CONF/lib" to the file ".

Run the POs program.

#./POS
./POS: Error while loading shared libraries: libpos. So: cannot open shared object file: no such file or directory
#

The system does not find the dynamic library libpos. So. Find the reason. After editing the configuration file/etc/lD. So. conf, the command ldconfig is not run, so the modification just made has not taken effect. Run ldconfig and try again.

# Ldconfig
#./POS
/Root/test/CONF/lib
#

The POS program runs successfully and prints the correct result.

Method 2: Use the environment variable LD_LIBRARY_PATH to specify the dynamic library search path.

You can specify the dynamic library search path by setting the environment variable LD_LIBRARY_PATH. When multiple dynamic library search paths are specified using this environment variable, the paths are separated by colons. This method is described in example 2 below.

Example 2:

Run the following command to create the dynamic library libpos. So using 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/N ");
}
Program 3: pos_env.c

The testing Executable File POs can use the target program POS obtained in Example 1 without re-compiling. Because the POs function prototype in pos_conf.c is the same as that in pos_env.c, and the dynamic library name is the same, it is like modifying the dynamic library POs and re-creating the library. This is also one of the advantages of using dynamic libraries.

Then, move the dynamic library libpos. So to the/root/test/CONF/lib directory.

# Mkdir-P/root/test/ENV/lib
# Mv libpos. So/root/test/ENV/lib
#

We can use export to set this environment variable. In all the commands after this environment variable is set, this environment variable is valid.

For example:

# Export LD_LIBRARY_PATH =/root/test/ENV/lib
#

However, for the sake of convenience, this article uses another method to set the environment variables, both adding the environment variable settings before the command, this environment variable is only valid for this command, after the command is executed, the environment variable is invalid. The following command is used:

# LD_LIBRARY_PATH =/root/test/ENV/lib./POS
/Root/test/ENV/lib
#

The program POS runs successfully and the printed result is "/root/test/ENV/lib", which is the result of running the function POs in the program pos_env.c. Therefore, the dynamic library found by the POs program is/root/test/ENV/lib/libpos. So.

Method 3: Specify the dynamic library search path of the program when compiling the target code.

You can also specify the dynamic library search path of the program when compiling the target code. This is specified through the GCC parameter "-wl,-rpath," (as shown in Example 3 ). When multiple dynamic library search paths are specified, the paths are separated by colons.

Example 3:

Run the following command to create the dynamic library libpos. So using the source pos. C (see program 4.

# Gcc-C pos. c
# Gcc-shared-fpci-O libpos. So pos. o
#

# Include <stdio. h>
Void pos ()
{
Printf (". // n ");
}
Procedure 4: pos. c

Because we need to specify the dynamic library search path of the executable file when compiling the target code, we need to use the GCC command to re-compile the source program main. C (see Program 2) to generate the executable file POS.

# Gcc-o pos main. C-L.-LPOS-wl,-rpath ,./
#

Run the POs program again.

#./POS
./
#

 

After the POs program runs successfully, the output result is exactly the result of the POs function in POS. C. Therefore, the dynamic library found by the POs program is./libpos. So.

The above describes three methods to specify the dynamic library search path, plus the default dynamic library search path/lib and/usr/lib, a total of five dynamic library search paths, so what is their search order?

When we introduced the above three methods, we created dynamic libraries respectively. /libpos. so,/root/test/ENV/lib/libpos. so and/root/test/CONF/lib/libpos. so. We use the source program pos_lib.c (see program 5) to create a dynamic library/lib/libpos. so, use the source program pos_usrlib.c (see program 6) to create a dynamic library/usr/lib/libpos. so.

# Include <stdio. h>
Void pos ()
{
Printf ("/lib/N ");
}
Program 5: pos_lib.c

# Include <stdio. h>
Void pos ()
{
Printf ("/usr/lib/N ");
}
Program 6: pos_usrlib.c

In this case, we get the five dynamic libraries libpos. So. These dynamic libraries have the same name and contain the public function POS with the same function prototype. However, the storage location is different from that printed by the public function POS. The public function POs in each dynamic library outputs the location where the dynamic library is stored. In this way, we can know which dynamic library is searched by the result of the executable file POs in execution Example 3, so as to obtain the search sequence of 1st dynamic databases, and then delete the dynamic library, then execute the POs program to obtain 2nd dynamic library search paths and then delete 2nd dynamic libraries. In this way, the order of Linux dynamic library search can be obtained. The correspondence between output results of program POS execution and the searched dynamic library is shown in Table 1:

Program POS output result Dynamic library used Specify the corresponding dynamic library search path
./ ./Libpos. So Dynamic library search path specified when compiling the target code
/Root/test/ENV/lib /Root/test/ENV/lib/libpos. So The dynamic library search path specified by the Environment Variable LD_LIBRARY_PATH
/Root/test/CONF/lib /Root/test/CONF/lib/libpos. So Dynamic library search path specified in the configuration file/etc/lD. So. conf
/Lib /Lib/libpos. So Default dynamic library search path/lib
/Usr/lib /Usr/lib/libpos. So Default dynamic library search path/usr/lib

Table 1: correspondence between POS output results and dynamic libraries

Create dynamic libraries and place them in the corresponding directories. The test environment is ready. Run the POs program and set the environment variable LD_LIBRARY_PATH in the command line.

# LD_LIBRARY_PATH =/root/test/ENV/lib./POS
./
#

According to the output result of the POs program, the first search is the dynamic library search path specified when the target code is compiled. Then we delete the dynamic library./libpos. So, and then run the above command to try again.

# Rm libpos. So
RM: Remove regular file 'libpos. so '? Y
# LD_LIBRARY_PATH =/root/test/ENV/lib./POS
/Root/test/ENV/lib
#

According to the output result of the POs program, the search path of the 2nd dynamic libraries is specified by the Environment Variable LD_LIBRARY_PATH. Delete/root/test/ENV/lib/libpos. So and run the preceding command.

# Rm/root/test/ENV/lib/libpos. So
RM: Remove regular file '/root/test/ENV/lib/libpos. so '? Y
# LD_LIBRARY_PATH =/root/test/ENV/lib./POS
/Root/test/CONF/lib
#

The search path of the 3rd dynamic libraries is the path specified by the configuration file/etc/lD. So. conf. Delete the dynamic library/root/test/CONF/lib/libpos. So and then run the preceding command.

# Rm/root/test/CONF/lib/libpos. So
RM: Remove regular file '/root/test/CONF/lib/libpos. so '? Y
# LD_LIBRARY_PATH =/root/test/ENV/lib./POS
/Lib
#

The search path of the 4th dynamic libraries is the default search path/lib. Delete the dynamic library/lib/libpos. So and run the preceding command.

# Rm/lib/libpos. So
RM: Remove regular file '/lib/libpos. so '? Y
# LD_LIBRARY_PATH =/root/test/ENV/lib./POS
/Usr/lib
#

The final dynamic library search path is the default search path/usr/lib.

Based on the above results, we can see that the search path sequence of the dynamic library is:

1. The dynamic library search path specified when the target code is compiled;

2. The dynamic library search path specified by the Environment Variable LD_LIBRARY_PATH;

3. The dynamic library search path specified in the configuration file/etc/lD. So. conf;

4. Default dynamic library search path/LIB;

5. The default dynamic library search path is/usr/lib.

When you specify the dynamic library search path in steps 1, 2, and 3, you can specify multiple dynamic library search paths. The search order is sequential Based on the specified path. This article does not illustrate this. If you are interested, you can refer to the method in this article for verification.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.