Tips for searching paths for Linux dynamic libraries

Source: Internet
Author: User
Tags function prototype

As we all know, the default search path for Linux dynamic libraries is/lib and/usr/lib. When a dynamic library is created, it is generally copied to both directories. When the program executes requires a dynamic library, and the dynamic library is not loaded into memory, the system will automatically go to the two default search paths to find the corresponding dynamic library file, and then load the file into memory, so that the program can use the function in the dynamic library, as well as other resources of the dynamic library. In Linux, the search path for a dynamic library can be specified in the following three ways, in addition to the default search path.

Method One: 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, where each behavior is a dynamic library search path. Each time you finish editing the file, you must run the command ldconfig for the modified configuration to take effect. We illustrate the method by example.

Example 1:

We use the following command to create the dynamic library libpos.so with the source program POS_CONF.C (see Program 1), and the detailed creation process is described in [1].

# gcc-c POS_CONF.C

# Gcc-shared-fpci-o libpos.so POS_CONF.O

#

#include

void Pos ()

{

printf ("/root/test/conf/lib\n");

}

Program 1:POS_CONF.C

Then compile the MAIN.C (see Program 2) to generate the target program POS with the following command.

# 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 directory/root/test/conf/lib.

# mkdir-p/root/test/conf/lib

# MV Libpos.so/root/test/conf/lib

#

Finally, edit the configuration file/etc/ld.so.conf and append a line "/root/test/conf/lib" to the file.

Try running the program POS.

#./pos

./pos:error while loading shared libraries:libpos.so:cannot open Shared object file:no such file or directory

#

Error, the system did not find the dynamic library libpos.so. Look for reasons, the original after editing the configuration file/etc/ld.so.conf, did not run the command ldconfig, so just the changes have not yet taken effect. We run Ldconfig and try again.

# Ldconfig

#./pos

/root/test/conf/lib

#

The program POS runs successfully and prints the correct results.

Method Two: Specify the dynamic library search path through the environment variable Ld_library_path.

You can also specify a dynamic library search path by setting the environment variable Ld_library_path. When multiple dynamic library search paths are specified through the environment variable, the paths are separated by a colon ":". This method is illustrated by the following example.

Example 2:

We use the following command to create a 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

void Pos ()

{

printf ("/root/test/env/lib\n");

}

Program 3:POS_ENV.C

The test executable POS can be used in example 1 to get the target program POS, which does not need to be compiled again. Because the function pos in POS_CONF.C and the function pos function in POS_ENV.C are identical, and the dynamic library name is the same, it is like recreating the library after modifying the dynamic library pos. This is also one of the advantages of using dynamic libraries.

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, which is valid for all commands after setting the environment variable.

For example:

# Export Ld_library_path=/root/test/env/lib

#

But for the sake of convenience, this article uses another method of setting the environment variable, which is only valid for the command before the command, and the environment variable is not effective until the command executes. As the following command:

# Ld_library_path=/root/test/env/lib./pos

/root/test/env/lib

#

The program POS runs successfully, and the result of printing is "/root/test/env/lib", which is the result of the function POS running in program POS_ENV.C. So the dynamic library that the program POS searches for is/root/test/env/lib/libpos.so.

Method Three: Specify the dynamic library search path for the program when compiling the target code.

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

Example 3:

We use the following command to create a dynamic library libpos.so with the source program POS.C (see program 4).

# gcc-c POS.C

# Gcc-shared-fpci-o libpos.so POS.O

#

#include

void Pos ()

{

printf ("./\n");

}

Program 4:POS.C

Because we need to specify the dynamic library search path of the executable when compiling the target code, we need to recompile the source program MAIN.C (see Program 2) with the GCC command to generate the executable pos.

# gcc-o POS main.c-l.-lpos-wl,-rpath,./

#

Try running the program POS again.

#./pos

./

#

The program POS runs successfully and the output is the result of the function pos running in POS.C. So the dynamic library that the program POS searches for IS./libpos.so.

The above describes three ways 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, then what is the order of their search?

In the introduction of the three methods described above, a dynamic library was created./libpos.so,/root/test/env/lib/libpos.so and/root/test/conf/lib/libpos.so. We then use the source program POS_LIB.C (see program 5) to create the dynamic library/lib/libpos.so, using the source program POS_USRLIB.C (see program 6) to create a dynamic library/usr/lib/libpos.so.

#include

void Pos ()

{

printf ("/lib\n");

}

Program 5:POS_LIB.C

#include

void Pos ()

{

printf ("/usr/lib\n");

}

Program 6:POS_USRLIB.C

So we get five dynamic library libpos.so, these dynamic libraries have the same name, and all contain common function pos of the same function prototype. But the location of the store differs from the result of the common function pos printing. The common function pos in each dynamic library outputs the location where the dynamic library is stored. In this way, we can find out which dynamic library it searched for by executing the results from the executable pos in Example 3, get the 1th Dynamic Library search order, then delete the dynamic library, execute the program POS, get the 2nd Dynamic Library search path, and delete the 2nd search dynamic library, so reciprocating, The sequencing of the Linux search dynamic Library will be available. The corresponding relationship between the output of the program POS execution and the search for the dynamic library is shown in table 1:

Dynamic Library search path specified by the dynamic library corresponding to the program POS output results

././libpos.so The dynamic library search path specified when compiling the target code

/root/test/env/lib/root/test/env/lib/libpos.so environment variable Ld_library_path the specified dynamic library search path

/root/test/conf/lib/root/test/conf/lib/libpos.so The dynamic library search path specified in configuration file/etc/ld.so.conf

/lib/lib/libpos.so The default dynamic library search path/lib

/usr/lib/usr/lib/libpos.so The default dynamic library search path/usr/lib

Table 1: Correspondence between program POS output and dynamic libraries

Create individual dynamic libraries and place them in the appropriate directory. The test environment is ready. Execute the program POS and set the environment variable Ld_library_path in the command line.

# Ld_library_path=/root/test/env/lib./pos

./

#

According to the output results of the program POS, the first search is the dynamic library search path specified when compiling the target code. Then we remove 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 results of the program POS, the 2nd Dynamic Library search path is specified by the environment variable Ld_library_path. We'll remove the/root/test/env/lib/libpos.so and run the above 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 for the 3rd dynamic library is the path specified by the configuration file/etc/ld.so.conf. Remove the dynamic library/root/test/conf/lib/libpos.so before running the above 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 for the 4th dynamic Library is the default search path,/lib. We then delete the dynamic library/lib/libpos.so and run the above command.

# rm/lib/libpos.so

Rm:remove regular file '/lib/libpos.so '? Y

# Ld_library_path=/root/test/env/lib./pos

/usr/lib

#

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

Combined with the above results, the order of search path search for dynamic library is as follows:

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

2. Environment variable LD_LIBRARY_PATH the specified dynamic library search path;

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

4. The default dynamic library search path/lib;

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

When you specify a dynamic library search path in the above 1, 2, 3, you can specify multiple dynamic library search paths, which are searched in the order of the specified paths. For this article no longer an example, interested readers can refer to the method of this article to verify.

Tips for searching paths for Linux dynamic libraries

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.