Linux dynamic library search path

Source: Internet
Author: User

2013.08.23 updated


Concept 1: The shared library is called a dynamic library.

In Linux, the executable file or dynamic library in ELF format has two fields: DT_RPATH and DT_RUNPATH (this is added to the new version ).

// This method is only written to the DT_RPATH domain gcc-g-Wall-o prog. c-Wl,-rpath,/home/dir1-ldemo // This method is written into DT_RPATH and DT_RUNPATHgcc-g-Wall-o prog. c-Wl, -- enable-new-dtags-Wl,-rpath,/home/dir1-ldemo

Compilation is divided into several stages.-W indicates the stage in which the following parameters are added,-Wl indicates that the following parameters are added in the link stage, and-rpath specifies the runtime dynamic path parameter for the linker.

The reason why the fields DT_RPATH and DT_RUNPATH need to be written later is that the previous version does not have DT_RUNTIME and is compatible with the old version.

2. The search path sequence of the Linux dynamic library is as follows:


0> If the DT_RUNPATH field is empty and DT_RPATH is not empty, search for the path list specified by DT_RPATH (multiple paths are separated by colons );

1> the dynamic library search path specified by the Environment Variable LD_LIBRARY_PATH. Multiple paths can be separated;

2> If DT_RUNPATH is not empty, search for the path list corresponding to DT_RUNPATH;

3> in/etc/ld. so. specified in the cache file. This file is a binary file. If you want to add it, edit the configuration file/etc/ld first. so. conf (the file is text), and then run ldconfig to generate a new/etc/ld. so. cache;

4> default dynamic library search path/lib;

5> default dynamic library search path/usr/lib.


The following are the suggestions for using LD_LIBRARY_PATH:

1> do not set LD_LIBRARY_PATH globally

2> if you must publish your code using a dynamic library and allow users to customize the installation path

  • Use the. o file to publish and reconnect them to the correct installation path during installation.
  • In the executable program, write a very lengthy, non-existent path (-Wl,-rpath), and then use a binary editor to correct the path during installation.

3> If LD_LIBRARY_PATH must be used, it must be packaged and used, for example, shell scripts.


Here is an example of my personal experience.

An Installed Software Package A is built on Platform 1.0. All dynamic library files of the Platform are located in/opt/lib64/

A new software package B is built on the Platform 2.0. All dynamic libraries of the Platform are located under the/lib directory of the software.

Start Software B and find the running error. Run the ldd command and find that the connected dynamic library is incorrect. The lib in the local directory should be connected, but the/opt/lib64/

It still cannot be started by setting LD_LIBRARY_PATH, because the path has been written to the executable file. You can use the strings command to view the strings in the binary.

Two solutions were found later:

1> use the binary editor to rewrite the path in the executable file. Change/opt/lib64 to/opt/lib65, so that/opt/lib65 cannot be found during running, then LD_LIBRARY_PATH takes effect.

2> before compiling B, modify the path in configure. ac so that the parameter passed to-Wl and-rpath is an impossible path.


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.