Search Path for loading the dynamic library by program execution:
[1] First, check whether the. dynamic segment contains an item named dt_rpath (which is a list of search directories of library files separated by colons ). This item is added by the command line switch or environment variable when the program is connected by a connector. It is often used in subsystems. For example, for database applications, we need to load some Program sets and support databases to a directory.
[2] Check whether the environment variable LD_LIBRARY_PATH exists (it is a list of library file search directories separated by colons ). This item helps developers create a new version of library, add its path to LD_LIBRARY_PATH, and use it with existing connectable programs to test new libraries,
[3] Check the cache file/etc/lD on the connector. so. conf, which contains a list of database names and paths. If the database name exists, the connector uses the corresponding path, you can use this search method to find most libraries (the file name does not need to be exactly the same as the requirements. For details, refer to the following "database version ").
If none of the above searches fail, the connector searches for the default path/usr/lib and/lib. If the library file is still not found, an error is displayed and exits.
After the connector finds the library file, open it first, read the elf header, and find the pointer pointing to each segment. The connector allocates space for library code segments and data segments and maps them to the memory, followed by BSS (no space allocated ).. Through the. dynamic segment of the library, the connector adds the symbol table of the Library to the symbol table chain. If other libraries on which the library depends are not loaded, add the Library to the load queue.
Dynamic Link Library search path during program Compilation:
During program compilation, if the dynamic link library on which the program depends is not installed in the system standard path:/lib/or/usr/lib, it is often prompted that the corresponding dynamic link library file cannot be found for execution, the reason is that the search path list of the Dynamic Link loader LD does not contain your library installation path.
The search paths of Linux dynamic link libraries are sorted by priority as follows:
1. The dynamic library search path specified when the target code is compiled;
(1) The GCC parameter "-wl,-rpath," is specified during compilation. When multiple dynamic library search paths are specified, the paths are separated by colons.
(2) Use the compile option-L to specify the directory where the dynamic link library is located (for Compiler search), and use the-L option to specify the abbreviated Dynamic Link Library name.
The difference between the two is:
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;
The first line of/etc/lD. So. conf has a reference command: Include lD. So. conf. d/*. conf
Therefore, the most elegant way is in LD. so. conf. d directory to create a configuration file that your program depends on. The configuration file content is the path of the dynamic link library that the program depends on, one path and one line.
After adding the configuration file, execute ldconfig to make it take effect.
4. Default dynamic library search path/LIB;
5. Default dynamic library search path/usr/lib;
How to share a Dynamic Linked Library
We can use the following three methods to share the Dynamic Link Library: (Note: All operations must be performed in the Super User State. Take my dynamic link library libmy. So sharing process as an example)
(1) copy the dynamic link library to the system shared directory, or create a connection for the dynamic link library under the system shared directory (both hard connection and symbolic connection are supported, commonly used symbolic connections ). the system shared directory refers to the directory of Linux dynamic link inventory, which contains/lib,/usr/lib and/etc/lD. so. A series of directories listed in the conf file.
# Cp libmy. So/lib
# Ldconfig
#
Or:
# Ln-s 'pwd'/libmy. So/lib
# Ldconfig
#
(2) append the Directory Name of the Dynamic Link Library to the configuration file/etc/lD. So. conf of the dynamic link library.
# PWD>/etc/lD. So. conf
# Ldconfig
#
(3) use the dynamic link library management command ldconfig to force it to search for a specified directory and update the cache file to facilitate dynamic loading.
# Ldconfig 'pwd'
#
It should be noted that this operation method is valid, but the effect is temporary for program testing. Once ldconfig is run again, the content of the cached file may change, the required dynamic link library may not be shared by the system. in comparison, the first two methods are reliable and are worthy of being used in the sharing of finalized dynamic link libraries. the first two methods also have a feature, that is, the last command is ldconfig, that is, the cache file needs to be updated to ensure that the dynamic link library sharing takes effect.