Several environment variables related to shared library (SO) in Linux

Source: Internet
Author: User
Several environment variables related to the shared library (SO) in Linux-Linux general technology-Linux programming and kernel information. The following is a detailed description. 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 libraries. 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 is the environment variable that everyone is most familiar with. It tells loader which directories can 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 is also very 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.

The environment variable LD _ DEBUG is quite fun. Sometimes it helps you find some 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 previous options combined

Statistics display relocation statistics

Unused determined unused DSOs

Help display this help message and exit

The environment variable BIND_NOW is consistent with the flag in dlopen, but the flag in dlopen is suitable for displaying the loading status, 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.

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.