[Linux] The program cannot find the dynamic library .so solution|View the information of the .so dynamic library|The order of loading the .so dynamic library

table of Contents

Can't find .so solution

Method 1: Add environment variables

Method 2: Copy the so file to the lib path

Method three: (recommended) add ldconfig to find the path

Method 4: Specify the dynamic library search path of the program when compiling the target code

Other commands

View the .so library that the program depends on

View the dynamic library connection process

View the version of a so link library

View the string information in the So file (the most common is the version number)

Dynamic library search order

Dynamic library [link time] path and [run time] path

Modify the library dependency path of the efl file (program, library file)


Can't find .so solution

The default search paths for Linux dynamic libraries are /lib and /usr/lib. In addition to the default search paths, you can also specify them in the following ways.

Original: https://www.it610.com/article/1295147005911310336.htm

Method 1: Add environment variables

Three ways to add environment variables

1. Add the environment variables of the current user's current terminal (temporary)

export LD_LIBRARY_PATH=/home/czd/...  #.so file path

2. Add the environment variables of the current user

Modify ~/.bashrc file and add environment variables at the end

vim ~/.bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/czd/...  #.so file path

Make it effective,

source ~/.bashrc

If it does not take effect, please restart

3. Add environment variables for all users

Modify the profile file and add environment variables at the end

vim /etc/profile
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/czd/...  #.so file path

Make it effective

source /etc/profile

If it does not take effect, please restart

Method 2: Copy the so file to the lib path

The so library of the linux system is generally stored in the "/usr/lib/" path, and the dynamic library can be copied to this path.

sudo cp liblibtest.so /usr/lib/

Immediate effect

Method three: (recommended) add ldconfig to find the path

Step 1. Edit the link configuration file

vim /etc/ld.so.conf, Confirm whether the content is as follows, if not, modify it as follows:

include /etc/ld.so.conf.d/*.conf

Step 2. Enter the /etc/ld.so.conf.ddirectory, create a *.conf file, the file name is arbitrary, the extension must be .conf

cd /etc/ld.so.conf/
vim libmy.conf 

Step 4. Inside the file, add the path of so, save and exit

/home/czd/eclipse-workspacee/calllib/Debug

Step 5. Effective period of execution order

sudo ldconfig

When the program searches for the so library at runtime, it will look for the added directory.

 

https://blog.csdn.net/qq_38350702/article/details/106128030

Method 4: Specify the dynamic library search path of the program when compiling the target code

 

There are two solutions. To specify the location of the link library when the first program is linked, use the -wl,-rpath=<link_path> parameter, and <link_path> is the path of the link library. Such as:

gcc -o foo foo.c -L. -lfoo -Wl,-rpath=/usr/mylib

The above is to specify that the link location is /usr/mylib. When executing ./foo, foo will go to /usr/mylib to find the libfoo.so library.

But in general, we use the following format

gcc -o foo foo.c -L$(prefix)/lib -lfoo -Wl,-rpath=$(prefix)/lib

 

Other commands

View the .so library that the program depends on

Check which dynamic libraries are needed to execute the program, and whether they can be found

ldd  /usr/bin/mount

[root@rdma64 lcx]# ldd ./ceph_perf_msgr_server_normal
        linux-vdso.so.1 =>  (0x00007ffc24dec000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f6f9a6ee000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f6f9a4ea000)
        libmymalloc.so => not found
        libjemalloc.so.1 => not found

        libsnappy.so.1 => /lib64/libsnappy.so.1 (0x00007f6f9a0e2000)
        libceph-common.so.0 => ./bak/libceph-common.so.0 (0x00007f6f912d5000)

 

To view which dynamic libraries are needed for the
executable program , use the command: readelf -d [executable program]   

 

View the dynamic library connection process

Check keep

ldconfig -v | grep keep

ldconfig -p | grep keep

View the version of a so link library

If you want the exact version, look at which specific file this library file is linked to:

/lib# file libhandle.so

libhandle.so : symbolic link to `libhandle.so.1 '

lib# file libhandle.so.1

libhandle.so.1: symbolic link to `libhandle.so.1.0.3 '

You can see that after two links, 1.0.3 in the final file name is the version number, other methods can only get the main version number

View the string information in the So file (the most common is the version number)

strings libc.so | grep Version

 

Dynamic library search order

 

Based on the above results, it can be seen that the search order of the search path of the dynamic library is:

1. The dynamic library search path specified when compiling the target code; //-L, -rpath and -rpath-link

2. The dynamic library search path specified by the environment variable LD_LIBRARY_PATH;

3. The dynamic library search path specified in the configuration file /etc/ld.so.conf;

4. The default dynamic library search path /lib;

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

When the dynamic library search path is specified in 1, 2, and 3 above, multiple dynamic library search paths can be specified, and the search sequence is searched in the order of the specified path.

http://blog.csdn.net/21aspnet/article/details/6724457

 

Dynamic library [link time] path and [run time] path

http://blog.csdn.net/q1302182594/article/details/42102961 (The difference between -L, -rpath and -rpath-link)

Modern linkers separate the link-time path and the run-time path when dealing with dynamic libraries. Users can

  • Specify the path of the library when compiling and linking with -L
  • Specify the path of the program runtime library through -R (or -rpath),

Greatly improve the flexibility of library applications.

For example, we are in the compilation environment gcc -o target -L /work/lib/zlib/ -llibz-1.2.3 (work/lib/zlib is the dynamic library directory of the compilation environment), after the target is compiled, we only need to download zlib Copy the library files to the default path of the operating environment. Or specify the search path through -rpath (or -R), LD_LIBRARY_PATH.

 

The options of the linker ld are -L, -rpath and -rpath-link, which roughly mean this:

-L: When "linking", the directory to find, that is, all libraries in the -lxxx option, will first be searched from the directory specified by -L, and then the default location. -L only specifies the path of the library when the program is compiled and linked, and does not affect the path of the library when the program is executed.

Both -rpath and -rpath-link can specify the path of the library when linking; but when running the executable file, the path specified by -rpath-link is no longer valid (the linker does not include the path of the library into the executable file) ,

The path specified by -rpath is still valid (because the linker has included the path of the library in the executable file.)

 

Modify the library dependency path of the efl file (program, library file)

Tool: patchelf
(RHEL already comes with the chrpath tool, you can use it directly. (yum install chrpath) But chrpath has a flaw. If the current system is x86_64, modifying i386 elf will report an error, patchelf has no such problem!)

 

The full name of rpath is run-time search path. It is included in all elf format files under Linux, especially executable files. It specifies the first priority position of executable files when looking for .so files. Use patchelf to modify its information. https://www.cnblogs.com/ar-cheng/p/13225342.html

View the libraries that the demo depends on:

$ readelf -d demo
 

更改程序或库运行时搜索依赖库的路径(多个路径用冒号隔开)

$ patchelf  --set-rpath   /opt/my-libs/lib:/other-libs   my-program

去掉冗余的依赖库路径(Shrink the RPATH of executables and libraries:)

$ patchelf  --shrink-rpath  my-program

去掉冗余的依赖库路径,但保留/usr/lib:/foo/lib前缀的路径

$ patchelf  --shrink-rpath  --allowed-rpath-prefixes  /usr/lib:/foo/lib  my-program

删除一些设置的依赖

$ patchelf  --remove-needed  libfoo.so.1 my-program 

增加依赖
$ patchelf  --add-needed        libfoo.so.1 my-program  

更换依赖。原本是依赖 liboriginal.so.1,改为依赖 libreplacement.so.1

$ patchelf  --replace-needed  liboriginal.so.1  libreplacement.so.1  my-program 

Change SONAME of a dynamic library:
$ patchelf  --set-soname  libnewname.so.3.4.5  path/to/libmylibrary.so.1.2.3 

Change the dynamic loader ("ELF interpreter") of executables
$ patchelf  --set-interpreter /lib/my-ld-linux.so.2 my-program 

Tutorial: https://blog.csdn.net/juluwangriyue/article/details/108617283

Instructions for the official website:

README.md
PatchELF is a simple utility for modifying existing ELF executables and libraries. In particular, it can do the following:

Change the dynamic loader ("ELF interpreter") of executables:
$ patchelf --set-interpreter /lib/my-ld-linux.so.2 my-program

Change the RPATH of executables and libraries:
$ patchelf --set-rpath /opt/my-libs/lib:/other-libs my-program

Shrink the RPATH of executables and libraries:
$ patchelf --shrink-rpath my-program

This removes from the RPATH all directories that do not contain a library referenced by DT_NEEDED fields of the executable or library. For instance, if an executable references one library libfoo.so, has an RPATH /lib:/usr/lib:/foo/lib, and libfoo.so can only be found in /foo/lib, then the new RPATH will be /foo/lib.

In addition, the --allowed-rpath-prefixes option can be used for further rpath tuning. For instance, if an executable has an RPATH /tmp/build-foo/.libs:/foo/lib, it is probably desirable to keep the /foo/lib reference instead of the /tmp entry. To accomplish that, use:

$ patchelf --shrink-rpath --allowed-rpath-prefixes /usr/lib:/foo/lib my-program

Remove declared dependencies on dynamic libraries (DT_NEEDED entries):
$ patchelf --remove-needed libfoo.so.1 my-program

This option can be given multiple times.
Add a declared dependency on a dynamic library (DT_NEEDED):
$ patchelf --add-needed libfoo.so.1 my-program

This option can be give multiple times.
Replace a declared dependency on a dynamic library with another one (DT_NEEDED):

$ patchelf --replace-needed liboriginal.so.1 libreplacement.so.1 my-program

This option can be give multiple times.

Change SONAME of a dynamic library:
$ patchelf --set-soname libnewname.so.3.4.5 path/to/libmylibrary.so.1.2.3

 

Guess you like

Origin blog.csdn.net/bandaoyu/article/details/113181179