Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of loading path and searching path of dynamic Link Library in Linux

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to solve the problem of loading path and search path of dynamic link library in Linux. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Introduction

Recently, after the server migration, lazy did not recompile nginx, directly. / nginx started, the result encountered the following problems:

"error while loading shared libraries"

This is because the required dynamic library is not in the search path of the dynamic linker ld.so.

Search order of ld.so dynamic shared Library

1. Specify the dynamic segment DT_RPATH in the ELF executable file; add the link parameter "- Wl,-rpath" to gcc to specify the dynamic library search path

2. The path specified by the environment variable LD_LIBRARY_PATH

3. The path to the cached dynamic library in / etc/ld.so.cache. You can add and delete paths by modifying the configuration file / etc/ld.so.conf (after modification, you need to run the ldconfig command)

4. Default / lib/

5. Default / usr/lib/

Commands related to dynamic link libraries

(1) ld is the linker of gcc.

(2) ldd is a program that checks the dependent libraries in the executable file. For example, if you want to find out which dynamic libraries are used by the main program, you can directly 
 ldd main.

(3) the changes used by ldconfig to update the file / etc/ld.so.conf take effect.

(4) nm is used to view the name of the function in the .so library. The tag T is the name generated in the dynamic library. Such as: nm / lib/libc*.so

The search path problem of dynamic Link Library under Linux

The search paths for Linux dynamic link libraries are sorted by priority as follows:

1. The dynamic library search path specified when compiling the object code

It is specified at compile time through the parameter "- Wl,-rpath," of gcc. When multiple dynamic library search paths are specified, the paths are separated by a colon ":".

two。 The dynamic library search path specified by the environment variable LD_LIBRARY_PATH

3. Dynamic library search path specified in configuration file / etc/ld.so.conf

The first line of / etc/ld.so.conf has a reference command: include ld.so.conf.d/*.conf

Therefore, the most elegant way is to create a configuration file that your program depends on in the ld.so.conf.d directory, and the content of the configuration file is the path to the dynamic link library that the program depends on, one path per line.

After adding the configuration file, execute ldconfig to make it effective.

4. Default dynamic library search path / lib

5. Default dynamic library search path / usr/lib

1. You can specify it with the LD_LIBRARY_PATH environment variable, which is similar to the PATH mechanism, which is more intuitive, and it can be solidified in bashrc or in your own .bashrc, which only works for this user.

2. If ld.so.cache is enabled, the system stores all referenced dynamic link libraries in / etc/ld.so.cache. The contents of this file can be specified through / etc/ld.so.conf; this is a relatively fixed mechanism that affects all global users; however, after changing the settings, you need root to call ldconfig to refresh it.

3, the default standard library path, this does not seem to need to be set. Includes / lib and / usr/lib. Of course, if it is a 64-bit system, it also includes / lib64 and / usr/lib64.

In other cases, if you only want to work on a particular application, you can specify the search path at compile time. The-Wl and-rpath parameters of gcc.

Oddly enough, / usr/local/lib and / usr/local/lib64 are not part of the standard path.

On "how to solve the Linux dynamic link library load path and search path problem" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report