Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to solve the problem that linux can't find the dynamic library?

Shulou Source: shulou.com Published: 2022-06-03 05:48:56 09月28日 Update

Dynamic libraries are shared libraries. When it comes to the problem that linux cannot find dynamic libraries, we must first understand the characteristics of dynamic libraries:

The ● dynamic library defers the loading of links to some library functions until the program is running.

● can realize resource sharing between processes. (therefore, dynamic libraries are also called shared libraries)

● makes it easy to upgrade some programs.

● can even really make link loading completely controlled by the programmer in the program code (display calls).

From the first point above, we can see that the dynamic library does not package the library files into the program in binary form like the static library, but dynamically loads the dynamic library when the program needs to call the corresponding library function. At this time, the system only knows the name of the dynamic library, but does not know the absolute path, so the system dynamic loader (dynamic linker/loader) is needed. For the executable program of elf, it is completed by ld-linux.so, which successively searches the DT_RPATH segment of the elf file (uncontrollable) = "environment variable LD_LIBRARY_PATH =" / etc/ld.so.cache file list = "/ lib/ and / usr/lib directory to find the library file and load it into memory.

Then what we need to do is to add the absolute path of the directory where the dynamic library resides to any order of the dynamic loader search order. Here's how to implement it:

Method 1: copy the self-made shared library to / lib or / usr/lib, not recommended

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

2.1. Temporarily add the absolute path of the directory where the dynamic library is located to the environment variable (invalidate with terminal shutdown)

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

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

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

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

Source ~ / bashrc or. ~ / .bashrc to make the configuration effective

System level: add the 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 the / etc/ld.so.cache file (2 steps to implement)

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 permission), which rebuilds the / etc/ld.so.cache file

These are the details of what to do if the linux dynamic library cannot be found. Please pay attention to other related articles for more information.

Tags: Dynamic path file directory program location method system variable library file environment run function that is level link search configuration problem between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology MariaDB Xiaomi Microsoft