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. WhenProgramWhen a dynamic library is required for execution, and the dynamic library is not loaded into the memory, the system will automatically go to the two default search paths to find the corresponding dynamic library file, then load the file to the memory, so that the program can use the function numbers 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: In the configuration file/etc/lD. So. confSpecifies the dynamic library search path.After editing the file,All must run commands LdconfigMake 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,
# Gcc-FPIC-shared-O libpos. So 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
Method 2: Use the environment variable LD_LIBRARY_PATHSpecifies the dynamic library search path.
Method 3: Compile the targetCodeSpecifies the dynamic library search path of the program.
You can also specify the dynamic library search path of the program when compiling the target code. -Wl, indicating that the following parameters will be passed to the link program LD (because GCC may automatically call LD ). Here, the GCC parameter "-wl,-rpath," is specified (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-fic-O libpos. So pos. o
#
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 =...: Lib
#
Search for the sequence of dynamic Databases
Dynamic library search path specified when compiling the target code
LD_LIBRARY_PATH
/Etc/lD. So. Cache
Default path/lib, And then/usr/lib.