如何解决Ubuntu的Linux发行版中的“cannot open shared object file”错误

时间:2020-01-09 10:45:30  来源:igfitidea点击:

从源代码安装程序时,加载共享库时出错。

error while loading shared libraries:
cannot open shared object file: No such file or directory

例如,我尝试使用FreeRADIUS服务器,报错:

radiusd: error while loading shared libraries:
libfreeradius-radius-2.1.10.so:
cannot open shared object file: No such file or directory

修复"无法打开共享库文件:无此类文件或者目录"错误

修复‘cannot open shared object file: No such file or directory' 错误

我们需要做的就是打开终端(Ctrl + Alt + T)并键入以下命令:

sudo /sbin/ldconfig -v

什么是共享对象文件?上面的命令如何解决该问题?

我们会看到,在C / C ++中,.so(共享对象)是已编译的库文件。之所以称为共享对象,是因为该库文件可以被多个程序共享。这些生成的库通常位于/ lib或者/ usr / lib目录中。

现在,如果我们想知道这个微小的命令是如何解决此问题的,那么我们应该阅读ldconfig的手册页,其中指出:

ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories (/lib and /usr/lib). The cache is used by the run-time linker, ld.so or ld-linux.so. ldconfig checks the header and filenames of the libraries it encounters when determining which versions should have their links updated.

解决"无法打开共享对象文件"错误的替代方法

假设我们看到此错误:

error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory

问题出在libgobject 2.0版上。版本号很重要,因为某些程序依赖于库的特定版本,如果找不到,就会报错。

现在,apt提供了搜索选项,可用于在安装之前搜索软件包并了解其版本。

theitroad@localhost:~$ apt search libgobject
Sorting... Done
Full Text Search... Done
librust-gobject-sys-dev/focal 0.9.0-2 amd64
  FFI bindings to libgobject-2.0 - Rust source code

现在,如果我们知道尝试运行Rust程序,则可能需要此librust-gobject-sys-dev软件包。但是,如果我们正在运行的Python程序抱怨它呢?

我们可以通过在搜索时从包名称中删除lib来扩大搜索范围。 lib表示库,并且库可以由通用包提供,该通用包可以命名为gobject-xyz。

在包名中搜索字符串(而不是描述)是一个好主意,以获得更简洁的结果。

theitroad@localhost:~$ apt search --names-only gobject
Sorting... Done
Full Text Search... Done
gobject-introspection/focal-updates 1.64.1-1~ubuntu20.04.1 amd64
  Generate interface introspection data for GObject libraries

libavahi-gobject-dev/focal 0.7-4ubuntu7 amd64
  Development headers for the Avahi GObject library

libavahi-gobject0/focal 0.7-4ubuntu7 amd64
  Avahi GObject library

libcairo-gobject-perl/focal,now 1.005-2 amd64 [installed,automatic]
  integrate Cairo into the Glib type system in Perl

libcairo-gobject2/focal,now 1.16.0-4ubuntu1 amd64 [installed,automatic]
  Cairo 2D vector graphics library (GObject library)

libghc-gi-gobject-dev/focal 2.0.19-1build1 amd64
  GObject bindings

libghc-gi-gobject-doc/focal,focal 2.0.19-1build1 all
  GObject bindings; documentation

在上面的截断输出中,我们必须查看该软件包是否与我们尝试运行的原始程序有关。我们还必须检查提供的库的版本。

确定正确的软件包后,按以下方式安装:

sudo apt package_name

安装后,我们可以再次运行ldconfig命令以更新高速缓存:

sudo /sbin/ldconfig -v