Article Title: Linux dynamic library (. so) search path. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
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
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
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.
[1] [2] Next page