Ubuntu 查询 so 归属的 package

时间:2023-03-08 21:19:27

.

.

.

.

.

今天 LZ 在运行一个程序的时候,出现找不到 so 库的情况:

>$ ./core
./core: error while loading shared libraries: libudev.so.: cannot open shared object file: No such file or directory
>$

可以看出来, core 这个程序依赖了一个叫做 libudev.so 的库,但是再系统中找不到这个库,那么就需要安装一下。

可是如果通过 apt-get 的方式安装的话,需要知道 libudev.so 在哪个软件包中才行。

那么问题来了,怎样查找一个 so 库在哪个软件包中呢?

通过 apt-file 命令就可以查找了。

首先 apt-file 命令不像 apt-get 命令一样,它在系统中是没有预装的,需要我们手动安装一下:

>$ sudo apt-get install apt-file
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
curl libconfig-file-perl libregexp-assemble-perl
The following NEW packages will be installed:
apt-file curl libconfig-file-perl libregexp-assemble-perl
upgraded, newly installed, to remove and not upgraded.
Need to get kB of archives.
After this operation, kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get: http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/main curl amd64 7.35.0-1ubuntu2.6 [123 kB]
Get: http://cn.archive.ubuntu.com/ubuntu/ trusty/universe libconfig-file-perl all 1.50-2 [10.1 kB]
Get: http://cn.archive.ubuntu.com/ubuntu/ trusty/universe libregexp-assemble-perl all 0.35-8 [81.2 kB]
Get: http://cn.archive.ubuntu.com/ubuntu/ trusty/universe apt-file all 2.5.2ubuntu1 [27.1 kB]
Fetched kB in 10s (22.2 kB/s)
Selecting previously unselected package curl.
(Reading database ... files and directories currently installed.)
Preparing to unpack .../curl_7.35.0-1ubuntu2.6_amd64.deb ...
Unpacking curl (7.35.-1ubuntu2.) ...
Selecting previously unselected package libconfig-file-perl.
Preparing to unpack .../libconfig-file-perl_1.-2_all.deb ...
Unpacking libconfig-file-perl (1.50-) ...
Selecting previously unselected package libregexp-assemble-perl.
Preparing to unpack .../libregexp-assemble-perl_0.-8_all.deb ...
Unpacking libregexp-assemble-perl (0.35-) ...
Selecting previously unselected package apt-file.
Preparing to unpack .../apt-file_2..2ubuntu1_all.deb ...
Unpacking apt-file (2.5.2ubuntu1) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up curl (7.35.-1ubuntu2.) ...
Setting up libconfig-file-perl (1.50-) ...
Setting up libregexp-assemble-perl (0.35-) ...
Setting up apt-file (2.5.2ubuntu1) ...
The system-wide cache is empty. You may want to run 'apt-file update'
as root to update the cache. You can also run 'apt-file update' as
normal user to use a cache in the user's home directory.
>$

安装好 apt-file 工具之后还不能马上进行查找,得先更新它的索引文件才行。

从上面的提示可以看出来,更新索引文件必须使用 root 身份,所以别忘了用 sudo 命令。

>$ sudo apt-file update
Downloading complete file http://cn.archive.ubuntu.com/ubuntu/dists/trusty/Contents-amd64.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
28.0M 28.0M :: :: --:--:--
Downloading complete file http://cn.archive.ubuntu.com/ubuntu/dists/trusty-updates/Contents-amd64.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
25.5M 25.5M :: :: --:--:--
Downloading Index http://cn.archive.ubuntu.com/ubuntu/dists/trusty/Contents-amd64.diff/Index:
No Index available.
Downloading complete file http://cn.archive.ubuntu.com/ubuntu/dists/trusty/Contents-amd64.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
28.0M --:--:-- --:--:-- --:--:--
File is up-to-date.
Downloading Index http://cn.archive.ubuntu.com/ubuntu/dists/trusty-updates/Contents-amd64.diff/Index:
No Index available.
Downloading complete file http://cn.archive.ubuntu.com/ubuntu/dists/trusty-updates/Contents-amd64.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
25.5M --:--:-- --:--:-- --:--:--
File is up-to-date.
Downloading Index http://cn.archive.ubuntu.com/ubuntu/dists/trusty/Contents-amd64.diff/Index:
No Index available.
Downloading complete file http://cn.archive.ubuntu.com/ubuntu/dists/trusty/Contents-amd64.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
28.0M --:--:-- --:--:-- --:--:--
File is up-to-date.
Downloading Index http://cn.archive.ubuntu.com/ubuntu/dists/trusty-updates/Contents-amd64.diff/Index:
No Index available.
Downloading complete file http://cn.archive.ubuntu.com/ubuntu/dists/trusty-updates/Contents-amd64.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
25.5M --:--:-- :: --:--:--
File is up-to-date.
Downloading complete file http://cn.archive.ubuntu.com/ubuntu/dists/trusty-backports/Contents-amd64.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
285k 285k :: :: --:--:--
Downloading complete file http://security.ubuntu.com/ubuntu/dists/trusty-security/Contents-amd64.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
22.6M 22.6M :: :: --:--:-- 100k
Downloading Index http://security.ubuntu.com/ubuntu/dists/trusty-security/Contents-amd64.diff/Index:
No Index available.
Downloading complete file http://security.ubuntu.com/ubuntu/dists/trusty-security/Contents-amd64.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
--:--:-- --:--:-- --:--:--
File is up-to-date.
Downloading Index http://security.ubuntu.com/ubuntu/dists/trusty-security/Contents-amd64.diff/Index:
No Index available.
Downloading complete file http://security.ubuntu.com/ubuntu/dists/trusty-security/Contents-amd64.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
--:--:-- --:--:-- --:--:--
File is up-to-date.
Ignoring source without Contents File:
http://extras.ubuntu.com/ubuntu/dists/trusty/Contents-amd64.gz
>$

耐心等待这些包更新完,然后就可以查找我们要的库了。好在这些索引文件不是很大,用不了多久就更新完毕了。

接下来使用 apt-file 命令查找我们的 so 在哪个 package 中。

>$ sudo apt-file search libudev.so
libudev-dev: /usr/lib/x86_64-linux-gnu/libudev.so
libudev1: /lib/x86_64-linux-gnu/libudev.so.
libudev1: /lib/x86_64-linux-gnu/libudev.so.1.3.
>$

本来 LZ 搜索的是 libudev.so.0,但是没有搜索到,所以就只好把末尾的 .0 去掉来搜索更大的范围。

在搜索的结果中,左侧的是 package,右侧就是 so 的路径,所以在查到 package 后通过 apt-get 命令安装它就可以了。

>$ sudo apt-get install libudev1
Reading package lists... Done
Building dependency tree
Reading state information... Done
libudev1 is already the newest version.
upgraded, newly installed, to remove and not upgraded.
>$

======================分割线====================

LZ 的运气总是不好,用 libudev.so.0 这个库来举栗子不够典型,因为这个库多少有些特殊,通过上面的安装结果可以看出来, libudev1 这个库并没有安装成功,因为它早已被安装好了。

既然我们运行的 ./core 程序中提示找不到 libudev.so.0,而我们的系统中只有一个 libudev.so.1,那么直接做一个符号链接就可以了。

>$ sudo ln -s /lib/x86_64-linux-gnu/libudev.so. /lib/x86_64-linux-gnu/libudev.so.
>$

在绝大多数情况下,通过 apt-file 命令根据 so 反查归属的 package,然后再通过 apt-get 命令安装这个 package 就可以了。

在少数情况下,比如 LZ 遇到的这种情况下,就需要通过一些变通的手段了。