Home>Article>Operation and Maintenance> What should I do if the Linux dynamic library cannot be found?

What should I do if the Linux dynamic library cannot be found?

藏色散人
藏色散人 Original
2020-05-25 10:13:08 2990browse

What should I do if the Linux dynamic library cannot be found?

What should I do if the Linux dynamic library cannot be found?

Solution to the problem that linux cannot find the dynamic library

Dynamic library is a shared library. When it comes to the problem of linux not being able to find the dynamic library, we must first understand Features of dynamic libraries:

Recommended: "linux tutorial"

● Dynamic libraries defer link loading of some library functions to the program period of operation.

● Can realize resource sharing between processes. (So dynamic libraries are also called shared libraries)

● Make it easy to upgrade some programs.

● Even the link loading can be completely controlled by the programmer in the program code (explicit call).

As can be seen from the first point above, the dynamic library does not package the library files into the program in binary form like the static library, but dynamically when the program runs to the time when the corresponding library function needs to be called. Load the dynamic library. At this time, the system only knows the name of the dynamic library, but does not know the absolute path. At this time, the system dynamic loader (dynamic linker/loader) is needed; for the elf executable program, it is used by ld-linux.so To complete, it successively searches the DT_RPATH section of the elf file (uncontrollable) => Environment variable LD_LIBRARY_PATH => /etc/ld.so.cache file list => /lib/ and /usr/lib directories to find the library file and load it. Memory.

Then what we need to do is to add the absolute path of the directory where the dynamic library is located to any sequence of the dynamic loader search sequence. Here is the specific implementation method:

Method 1: Copy Go to /lib or /usr/lib for your own shared libraries, not recommended

Method 2: Add the absolute path of the directory where the dynamic library is located to the system environment variable

2.1. Add the dynamic library The absolute path of the directory is temporarily added to the environment variable (it becomes invalid when the terminal is closed)

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/chiliast/homework/day03/shared/lib

2.2. Set the absolute path of the directory where the dynamic library is located to ~/.bashrc or /etc/profile (effective permanently)

User level: append the library path to the end of the ~/.bashrc file

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/chiliast/homework/day03/shared/lib

source ~/.bashrc or . ~/.bashrc to make the configuration take effect

System level: append Library path to the end of the /etc/profile file

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/chiliast/homework/day03/shared/lib

source /etc/profile or ./etc/profile to make the configuration effective

Method 3: Add the absolute path of the directory where the dynamic library is located to / etc/ld.so.cache file (2-step implementation)

Edit the /etc/ld.so.conf file and add the path to the directory where the library file is located

Run ldconfig (requires super user permissions), this command will rebuild the /etc/ld.so.cache file

The above is the detailed content of What should I do if the Linux dynamic library cannot be found?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn