The Linux shared library has a long history and is no longer a new concept. Everyone knows how to compile, connect, and dynamically load (dlopen/dlsym/dlclose)
Shared library. However, many people, even some experts, may have a vague understanding of some environment variables related to the shared library. Of course, shared libraries can be used even if you do not know these environment variables. However, if you know these variables, they may be used better. The following describes some common environment variables that may help you:
LD_LIBRARY_PATH
This environment variable is most familiar to everyone. It tells loader which directories can be used to find shared libraries. You can set multiple search directories separated by colons. In Linux, another method is provided to accomplish the same function. You can add these directories to/etc/lD. so. conf, or in/etc/lD. so. conf. d. Create a file and add the directory to the file. Of course, this is globally effective within the system scope, while environment variables are only valid for the current shell. By convention, unless you specify in the above method, the loader will not find the shared library in the current directory, just as the shell will not find the executable file currently.
Ld_preload
This environment variable is particularly useful for programmers. It tells loader that when parsing the function address, the functions in the shared library specified in ld_preload are preferentially used. This provides convenience for debugging. For example, for C/C ++ programs, memory errors are the most difficult to solve. A common practice is to reload the malloc series functions, but it is troublesome to re-compile the program. By using the ld_preload mechanism, you do not need to re-compile the packaging function library into a shared library, and add the name of the shared library to ld_preload. These packaging functions will be automatically called. In Linux, another method is provided to implement the same function. You can write the file name of the shared library to be preferentially loaded in/etc/lD. So. preload. Of course, this is globally effective within the system scope, while environment variables are only valid for the current shell.
LD _ debug
This environment variable is quite fun. Sometimes it can be used to help you find difficult problems with shared libraries (such as problems caused by functions of the same name ). At the same time, you can also learn some knowledge about the shared library loading process. Its parameters are as follows:
Libs display library search paths
Reloc display relocation Processing
Files display progress for input file
Symbols display symbol table Processing
Bindings display information about symbol binding
Versions display version Dependencies
All all previous options combined
Statistics display relocation statistics
Unused determined unused DSOs
Help display this help message and exit
Bind_now
This environment variable is consistent with the flag in dlopen, except that the flag in dlopen is suitable for displaying loading conditions, while bind_now/bind_not is suitable for implicit loading.
Ld_profile/ld_profile_output: Generate profile data for the specified shared library. ld_profile specifies the name of the shared library. ld_profile_output specifies the location of the output profile file, which is a directory and must exist, the default directory is/var/tmp/or/var/profile. Using profile data, you can obtain statistics on the usage of functions in the shared library.
Environment variables related to shared libraries (SO) in Linux
The Linux shared library has a long history and is no longer a new concept. Everyone knows how to compile, connect, and dynamically load (dlopen/dlsym/dlclose)
Shared library. However, many people, even some experts, may have a vague understanding of some environment variables related to the shared library. Of course, shared libraries can be used even if you do not know these environment variables. However, if you know these variables, they may be used better. The following describes some common environment variables that may help you:
LD_LIBRARY_PATH
This environment variable is most familiar to everyone. It tells loader which directories can be used to find shared libraries. You can set multiple search directories separated by colons. In Linux, another method is provided to accomplish the same function. You can add these directories to/etc/lD. so. conf, or in/etc/lD. so. conf. d. Create a file and add the directory to the file. Of course, this is globally effective within the system scope, while environment variables are only valid for the current shell. By convention, unless you specify in the above method, the loader will not find the shared library in the current directory, just as the shell will not find the executable file currently.
Ld_preload
This environment variable is particularly useful for programmers. It tells loader that when parsing the function address, the functions in the shared library specified in ld_preload are preferentially used. This provides convenience for debugging. For example, for C/C ++ programs, memory errors are the most difficult to solve. A common practice is to reload the malloc series functions, but it is troublesome to re-compile the program. By using the ld_preload mechanism, you do not need to re-compile the packaging function library into a shared library, and add the name of the shared library to ld_preload. These packaging functions will be automatically called. In Linux, another method is provided to implement the same function. You can write the file name of the shared library to be preferentially loaded in/etc/lD. So. preload. Of course, this is globally effective within the system scope, while environment variables are only valid for the current shell.
LD _ debug
This environment variable is quite fun. Sometimes it can be used to help you find difficult problems with shared libraries (such as problems caused by functions of the same name ). At the same time, you can also learn some knowledge about the shared library loading process. Its parameters are as follows:
Libs display library search paths
Reloc display relocation Processing
Files display progress for input file
Symbols display symbol table Processing
Bindings display information about symbol binding
Versions display version Dependencies
All all previous options combined
Statistics display relocation statistics
Unused determined unused DSOs
Help display this help message and exit
Bind_now
This environment variable is consistent with the flag in dlopen, except that the flag in dlopen is suitable for displaying loading conditions, while bind_now/bind_not is suitable for implicit loading.
Ld_profile/ld_profile_output: Generate profile data for the specified shared library. ld_profile specifies the name of the shared library. ld_profile_output specifies the location of the output profile file, which is a directory and must exist, the default directory is/var/tmp/or/var/profile. Using profile data, you can obtain statistics on the usage of functions in the shared library.