Linux 查看程序(动态库)、进程的依赖库的方法

Linux 查看程序(动态库)、进程的依赖库的方法Linux查看程序(动态库)、进程的依赖库的方法_如何查看可执行文件引用的库

当我们在linux上部署自己开发的项目程序时,项目代码与依赖库完整拷贝过去之后,直接运行我们自己的可执行程序,往往提示找不到所相关的.so库,会报错。
系统只会去默认搜寻目录(/lib和/usr/lib)下, 以及动态库配置文件(/etc/ld.so.conf和/etc/ld.so.conf.d/*.conf)里所列的目录下, 搜索可共享的动态库(格式如lib*.so*), 进而创建出动态装入程序*(ld.so)*所需的连接和缓存文件。因此,需要我们手动将路径添加进去。

1. 利用ldd查看可执行程序(动态库)的依赖库

使用 ldd -r XXX 查询动态库的依赖。关于undefined symbol使用 C++filt 查看

[user@localhost dist]$ ldd /usr/bin/bash
        linux-vdso.so.1 =>  (0x00007ffdbe9e0000)
        libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007fa7da66c000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fa7da468000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fa7da09a000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fa7da896000)

[user@localhost ocr_server]$ ldd libgims.so
        linux-vdso.so.1 =>  (0x00007ffca0de1000)
        libopencv_imgproc.so.405 => /usr/local/lib64/libopencv_imgproc.so.405 (0x00007f6dbb5ce000)
        libopencv_core.so.405 => /usr/local/lib64/libopencv_core.so.405 (0x00007f6dba3df000)
        libopencv_imgcodecs.so.405 => /usr/local/lib64/libopencv_imgcodecs.so.405 (0x00007f6db9e57000)
        libonnxruntime.so.1.11.1 => /home/user/onnxruntime/lib/libonnxruntime.so.1.11.1 (0x00007f6db8fef000)
        libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f6db8ce7000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f6db89e5000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f6db87cf000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f6db8401000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f6db81e5000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f6db7fe1000)
        libpng15.so.15 => /lib64/libpng15.so.15 (0x00007f6db7998000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f6dbd551000)

[wangsp@localhost ocr_server]$ ldd libASL_gcn_label.so  `{ 
    部署到生产环境出现错误:not found}`
        linux-vdso.so.1 => not found  
        libopencv_imgproc.so.405 => not found
        libopencv_core.so.405 => not found
        libopencv_imgcodecs.so.405 => not found
        libonnxruntime.so.1.11.1 => not found
        libstdc++.so.6 => /usr/local/lib64/libstdc++.so.6 (0x00007f1013e6a000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f1013b68000)
        libgcc_s.so.1 => /usr/local/lib64/libgcc_s.so.1 (0x00007f1013950000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f1013582000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f1013366000)
        libjpeg.so.62 => /lib64/libjpeg.so.62 (0x00007f1012aef000)
        libpng15.so.15 => /lib64/libpng15.so.15 (0x00007f10128c4000)
        libtiff.so.5 => /lib64/libtiff.so.5 (0x00007f1012650000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f101785f000)
        libjbig.so.2.0 => /lib64/libjbig.so.2.0 (0x00007f1012444000)

方法:

  1. /etc/ld.so.conf.d 目录下创建 XXX_server.conf 文件,将该软件依赖的库路径添加进去
    echo /home/user/Project/XXX_server/ > /etc/ld.so.conf.d/XXX_server.conf
    执行生效
    ldconfig

  2. 或者:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/custom/path
    或者:echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/your/custom/path/' >> ~/.bashrc

2. 利用pldd获取进程的内存映射信息,进程的依赖共享库

如下XXX即需要查询的程序名称
pldd $(ps -ef | grep XXX| grep -v grep | awk '{print $2}')

[user@localhost laun]$ sudo pldd $(ps -ef | grep XXX | grep -v grep | awk '{print $2}')
3172:   /home/user/laun/dist/XXX/XXX
linux-vdso.so.1
/lib64/libdl.so.2
/home/user/laun/dist/XXX/libz.so.1
/lib64/libpthread.so.0
/lib64/libc.so.6
/lib64/ld-linux-x86-64.so.2
/home/user/laun/dist/XXX/libpython3.6m.so.1.0
/lib64/libutil.so.1
/lib64/libm.so.6
/home/user/laun/dist/XXX/lib-dynload/_struct.cpython-36m-x86_64-linux-gnu.so
/home/user/laun/dist/XXX/lib-dynload/zlib.cpython-36m-x86_64-linux-gnu.so
...
/home/user/laun/dist/XXX/lib-dynload/_json.cpython-36m-x86_64-linux-gnu.so
/home/user/laun/dist/XXX/markupsafe/_speedups.cpython-36m-x86_64-linux-gnu.so
/home/user/laun/dist/XXX/lib-dynload/unicodedata.cpython-36m-x86_64-linux-gnu.so
./XXX/libgims.so
/usr/local/lib64/libopencv_imgproc.so.405
/usr/local/lib64/libopencv_core.so.405
/usr/local/lib64/libopencv_imgcodecs.so.405
/home/user/onnxruntime-linux-x64-1.11.1/lib/libonnxruntime.so.1.11.1
/lib64/libpng15.so.15
./XXX/libgmc.so
./XXX/libgcn_label.so
./XXX/libASL_gcn_label.so
./XXX/libiisis.so
./XXX/libit_dwmp.so
./XXX/libit_dwmp2.so
./XXX/liblgbm.so
/home/user/laun/dist/XXX/gevent/_gevent_c_imap.cpython-36m-x86_64-linux-gnu.so
/home/user/laun/dist/XXX/lib-dynload/_multiprocessing.cpython-36m-x86_64-linux-gnu.so

3. 利用pmap工具查询未知的可执行程序的依赖库

[user@localhost dist]$ objdump -p /usr/local/php/bin/php |grep NEEDED
  NEEDED               libcrypt.so.1
  NEEDED               librt.so.1
  NEEDED               libmysqlclient.so.18
  NEEDED               libmcrypt.so.4
  NEEDED               libiconv.so.2
  NEEDED               libcurl.so.4
  NEEDED               libm.so.6
  NEEDED               libdl.so.2
  NEEDED               libnsl.so.1
  NEEDED               libxml2.so.2
  NEEDED               libz.so.1
  NEEDED               libssl.so.10
  NEEDED               libcrypto.so.10
  NEEDED               libpthread.so.0
  NEEDED               libc.so.6
  NEEDED               libresolv.so.2

4. 利用pmap查看正在运行时的进程的依赖库

[user@localhost ~/software/pldd]$ ps -ef|grep php-fpm
root     26534     1  0  2014 ?        00:01:34 php-fpm: master process (/usr/local/php-5.3.29/etc/php-fpm.conf)
nobody   26535 26534  0  2014 ?        00:00:04 php-fpm: pool www          
nobody   26536 26534  0  2014 ?        00:00:05 php-fpm: pool www          
root     30510 30324  0 00:39 pts/0    00:00:00 grep php-fpm


[user@localhost ~/software/pldd]$ pmap 26534 |head 
26534:   php-fpm: master process (/usr/local/php-5.3.29/etc/php-fpm.conf)
0000000000400000  10452K r-x--  /usr/local/php-5.3.29/sbin/php-fpm
0000000001035000     76K rw---  /usr/local/php-5.3.29/sbin/php-fpm
0000000001048000    104K rw---    [ anon ]
0000000002a65000   3448K rw---    [ anon ]
000000311c600000    388K r-x--  /usr/lib64/libssl.so.1.0.1e
000000311c661000   2048K -----  /usr/lib64/libssl.so.1.0.1e
000000311c861000     16K r----  /usr/lib64/libssl.so.1.0.1e
000000311c865000     28K rw---  /usr/lib64/libssl.so.1.0.1e
000000311ce00000     92K r-x--  /lib64/libpthread-2.12.so

5. ln 命令

ln(link 其实link命令的缩写) 具体使用: ln [选项] 目标文件

选项有如下

-s 使用软连接

-b 删除,覆盖之前建立的链接

-d 允许超级用户制作目录的硬链接

-f 强制执行

-i 交互模式,文件存在则提示用户是否覆盖

-n 把符号链接视为一般目录

-v 显示详细的处理过程

6. version GLIBCXX_3.4.20 not found 错误解决

参考:Linux 批量依赖库拷贝(ldd)
version GLIBCXX_3.4.20 not found 错误解决
libstdc++.so.6: version `GLIBCXX_3.4.21‘ not found

7. 使用dlopen加载动态库

https://www.cnblogs.com/0xzhang/p/14460925.html
https://www.cnblogs.com/Anker/p/3746802.html

今天的文章Linux 查看程序(动态库)、进程的依赖库的方法分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/62604.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注