简体   繁体   English

如何以编程方式检查依赖库的程序是否可以在Linux系统中运行?

[英]How to programmatically check whether a library dependent program can run in a Linux system?

I am having a program(binary) which depends upon libraries such as pthread, sqlite3,libcrypto and libcurl.I want to run this program in multiple user PCs.How to programmatically check whether the dependencies are met, before installing the binary? 我有一个依赖于pthread,sqlite3,libcrypto和libcurl等库的程序(二进制),我想在多台用户PC上运行此程序。在安装二进制文件之前,如何以编程方式检查是否满足依赖关系?

./configure cannot be be used as it is for the purpose of building the program as mentioned in , Making os independent configure file which checks for curl dependency . ./configure不能直接用于构建程序,如os独立的配置文件中检查curl依赖关系中所述 If I am not wrong .deb and .rpm have there own methods for this. 如果我没有记错的话,.deb和.rpm有自己的方法。

Can anybody please tell me what is the method they are following for this.Is it just a file name check? 谁能告诉我他们使用的方法是什么,这仅仅是文件名检查吗? For example,if I have build the program using libcurl.so.3, whether it checks that the system in which it will be running is having libcurl.so.3 as a regular file or a simulink.Or any other check is there for the libraries? 例如,如果我使用libcurl.so.3来构建程序,则它是否检查将运行其的系统是将libcurl.so.3作为常规文件还是simulink。或者是否存在其他任何检查图书馆?

What is the reliable method for checking the dependencies, while installing and running a binary? 在安装和运行二进制文件时,检查依赖项的可靠方法是什么?

Building a package 建立一个包裹

You could distribute your program as .deb or .rpm package. 您可以将程序作为.deb.rpm软件包分发。 Both formats support specifying dependencies that need to be present: 两种格式都支持指定需要存在的依赖关系:

Checking manually using ldd 使用ldd手动检查

You could use ldd(1) to check whether necessary shared libraries are installed and how they are resolved: 您可以使用ldd(1)检查是否安装了必需的共享库以及如何解决它们:

$ ldd /usr/bin/xterm
        linux-vdso.so.1 =>  (0x00007fff649ff000)
        libXft.so.2 => /usr/lib/x86_64-linux-gnu/libXft.so.2 (0x00007fc5195cd000)
        libXaw.so.7 => /usr/lib/x86_64-linux-gnu/libXaw.so.7 (0x00007fc51935b000)
        libutempter.so.0 => /usr/lib/libutempter.so.0 (0x00007fc519158000)
        libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007fc518f2f000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc518ba2000)
        libfontconfig.so.1 => /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007fc51896a000)
        libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007fc51862f000)
        libXmu.so.6 => /usr/lib/x86_64-linux-gnu/libXmu.so.6 (0x00007fc518415000)
        libXt.so.6 => /usr/lib/x86_64-linux-gnu/libXt.so.6 (0x00007fc5181ad000)
        libICE.so.6 => /usr/lib/x86_64-linux-gnu/libICE.so.6 (0x00007fc517f92000)
        libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007fc517cf3000)
        libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007fc517ae9000)
        libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007fc5178d7000)
        libXpm.so.4 => /usr/lib/x86_64-linux-gnu/libXpm.so.4 (0x00007fc5176c6000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fc5197f8000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fc5174ae000)
        libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007fc517284000)
        libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007fc517064000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc516e5f000)
        libSM.so.6 => /usr/lib/x86_64-linux-gnu/libSM.so.6 (0x00007fc516c58000)
        libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007fc516a54000)
        libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007fc51684f000)
        libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007fc51664a000)

When required libraries are not found, "not found" is printed: 当找不到所需的库时,将显示“找不到”:

$ ldd bar
        linux-vdso.so.1 =>  (0x00007fffde7ff000)
        libfoo.so => not found
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5954eae000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f5955251000)

Unfortunately, ldd does not return useful exit code in that case . 不幸的是, 在这种情况下ldd 不会返回有用的退出代码

Keep it simple, stupid 保持简单,愚蠢

You could just try to run your program and when it fails due to missing libraries, then... you know that you are missing some libraries;) 您可以尝试运行程序,但是由于缺少库而导致程序失败时,那么...您知道您缺少某些库;)

RPM will automatically detect used libraries and put needed Requires into final RPM package. RPM将自动检测已使用的库,并将所需的需求放入最终的RPM软件包中。 You can check it using: 您可以使用以下方法进行检查:

rpm -qpR foo.rpm

and it should print something like: 它应该打印如下内容:

libc.so.6(GLIBC_2.8)(64bit)
libdl.so.2()(64bit)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM