c++ 加载共享库时出错:libgsl.so.0:无法打开共享对象文件:没有这样的文件或目录

hfyxw5xn  于 11个月前  发布在  其他
关注(0)|答案(9)|浏览(121)

我使用gsl。在我编译了我的.cpp文件并运行它之后,我遇到了下面的错误:
加载共享库时出错:libgsl.so.0:无法打开共享对象文件:没有这样的文件或目录
我发现这个问题相同:https://groups.google.com/forum/#!topic/cortex_var/6vluX 7 pP 0 Sk & Linux错误加载共享库时:无法打开共享目标文件:没有这样的文件或目录& http://www.gnu.org/software/gsl/manual/html_node/Shared-Libraries.html
我已经按照上面的链接写了,但错误仍然存在。有人能帮助我吗?

1hdlvixo

1hdlvixo1#

要使其工作,请执行以下步骤
启动Borne Shell

$LD_LIBRARY_PATH= path to your gsl lib folder inside the gsl installation folder
$export LD_LIBRARY_PATH

字符串
现在运行可执行文件
应该没问题

vfwfrxfs

vfwfrxfs2#

首先,您需要定位文件(libgsl.so.0)。您可以使用find命令来完成此操作:

sudo find / -name "libgsl.so.0"

字符串
让我们假设,该文件位于/usr/local/lib中。(如果没有找到该文件,请安装相应的软件包 * 或 * 下载源代码,构建并安装它。)现在,您有两个选择:
(1)快速和肮脏:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LD_LIBRARY_PATH


这会将库的路径添加到环境变量中。此选项的缺点是,它仅对当前会话有效。它对其他用户无效。一旦您注销并再次登录,它将不起作用。
(2)永久性:
检查/etc/ld.so.conf。如果/usr/local/lib没有列出,请添加它。现在,运行ldconfig来检测共享目标文件并将其添加到系统范围的索引中。

nwlls2ji

nwlls2ji3#

根据我的经验,fastStructure依赖于gsl 1.6,但不是最新版本。

wget http://gnu.mirror.vexxhost.com/gsl/gsl-1.6.tar.gz
tar -zxvf gsl-1.6.tar.gz
cd gsl-1.16
./configure
make
sudo make install

字符串
将这些行添加到主目录下的.bashrc文件中。

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export CFLAGS="-I/usr/local/include"
export LDFLAGS="-L/usr/local/lib"


然后,运行source ~/.bashrc来设置这些环境变量。
当我将版本从最新版本更改为1.6时,它工作正常。

m3eecexj

m3eecexj4#

我在Arch Linux上使用Krita时也遇到了同样的错误。

ln /usr/lib/libgsl.so /usr/lib/libgsl.so.0

字符串
然后就修好了

70gysomp

70gysomp5#

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/shg047/software/gsl/lib/

字符串
例如:to-mr:加载共享库时出错:libgsl.so.19:无法打开共享对象文件:没有这样的文件或目录

jmp7cifd

jmp7cifd6#

你试过更新你的库吗?我试着运行的程序只需要一个更新的gsl版本(我有1.9.5,而它需要2.0.0或更新)。
如果你在arch上,你可以运行:

yaourt gsl

字符串
并选择合适的一个。

xdnvmnnf

xdnvmnnf7#

你可以在makefile中使用gsl-config --libs,或者在命令行中链接gsl库。只要输入gsl-config,你就可以找到它提供给你的选项。选择你需要的选项,你会发现编译和链接过程比以前容易得多。因此,当我在终端中输入gsl-config --libs时,我得到-L/usr/local/lib -lgsl -lgslcblas -lm。虽然它非常简单,首先你应该知道你的gsl安装在哪里。你可以将目录添加到PATH环境变量中,或者使用绝对路径来执行gsl-config

juud5qan

juud5qan8#

我需要libgsl.so.19
/snap/inkscape/current/bin/inkscape: error while loading shared libraries: libgsl.so.19: cannot open shared object file: No such file or directory
我解决了它:
1.安装Anaconda
1.搜索libgsl.so.19并在~/anaconda 3/lib中找到它
1.运行LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/anaconda3/lib(最佳add it to ~/.basrc

0sgqnhkj

0sgqnhkj9#

Max Ghenis和ManuelAtWork的解决方案帮助我解决了另一个共享库的相关问题。我想在这里记录我的步骤,以防它帮助其他人。

TL;DR

即使LD_LIBRARY_PATH包含/usr/local/lib,并且您的共享对象库(.so文件)也位于其中,您仍然可能会收到类似以下错误:
error while loading shared libraries: <INSERT LIBRARY NAME>.so.0: cannot open shared object file: No such file or directory
如果链接器缓存/etc/ld.so.cache在安装库后尚未重建。
您可以通过运行ldconfig(或sudo ldconfig,取决于/etc目录权限,这些权限可能只能由root用户写入)来修复此问题。
如果成功,ldconfig将不打印输出。要了解运行ldconfig时的详细输出,请使用-v(或--verbose)标志。
如果您曾经遇到过此错误,然后在您进行故障排除时它神奇地自行修复,那么可能是因为您遵循了某人的建议,并执行了以下操作
ldconfig -v | grep <INSERT LIBRARY NAME>
以确保.so文件位于LD_LIBRARY_PATH中列出的目录中,如/usr/local/lib。如果您已经确认该目录包含在/etc/ld.so.conf/etc/ld.so.conf.d/*.conf中,但ldconfig -v仍然在详细模式下 * 运行 * ldconfig,这可能会重新构建/etc/ld.so.cache,并修复您的问题,则执行此故障排除步骤是合理的。
如果您的错误在一段时间后神秘地自行修复,而无需运行ldconfig,则系统上的其他东西必须从那时起运行它-例如启动脚本,计划任务或其他软件的安装脚本。

详情

我最近安装了libhttpserver,按照其GitHub存储库上的说明,遵循make命令的标准步骤。我能够针对库编译外部程序,但当我运行我的程序时,我收到了与动态链接相关的相同错误。

$ g++ -o server server.c `pkg-config --cflags --libs libhttpserver`
./server

字符串
error while loading shared libraries: libhttpserver.so.0: cannot open shared object file: No such file or directory
我确认pkg-config知道链接器标志和库名称,这意味着libhttpserver.pc文件已经正确安装。

$ pkg-config --cflags --libs libhttpserver
-I/usr/local/include -I/usr/local/include/httpserver -I/usr/include/p11-kit-1 -L/usr/local/lib -lhttpserver -lmicrohttpd


我注意到了-L/usr/local/lib标志,并确认了共享目标文件位于那里。

$ find /usr/local/lib -name '*httpserver*'
/usr/local/lib/libhttpserver.so.0
/usr/local/lib/libhttpserver.la
/usr/local/lib/libhttpserver.so.0.19.0
/usr/local/lib/pkgconfig/libhttpserver.pc
/usr/local/lib/libhttpserver.a
/usr/local/lib/libhttpserver.so


根据该线程的建议,我在ldconfig -v的输出中搜索了库名称

$ ldconfig -v |grep libhttpserver
/sbin/ldconfig.real: Path `/usr/lib/x86_64-linux-gnu' given more than once
(from /etc/ld.so.conf.d/x86_64-linux-gnu.conf:4 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3)
/sbin/ldconfig.real: Path `/usr/lib32' given more than once
(from /etc/ld.so.conf.d/zz_i386-biarch-compat.conf:3 and /etc/ld.so.conf.d/zz_i386-biarch-compat.conf:2)
/sbin/ldconfig.real: Path `/usr/libx32' given more than once
(from /etc/ld.so.conf.d/zz_x32-biarch-compat.conf:3 and /etc/ld.so.conf.d/zz_x32-biarch-compat.conf:2)
/sbin/ldconfig.real: Path `/lib/x86_64-linux-gnu' given more than once
(from <builtin>:0 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3)
/sbin/ldconfig.real: Path `/usr/lib/x86_64-linux-gnu' given more than once
(from <builtin>:0 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3)
/sbin/ldconfig.real: Path `/usr/lib' given more than once
(from <builtin>:0 and <builtin>:0)
    libhttpserver.so.0 -> libhttpserver.so.0.19.0
/sbin/ldconfig.real: /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 is the dynamic linker, ignoring

/sbin/ldconfig.real: /lib32/ld-linux.so.2 is the dynamic linker, ignoring

/sbin/ldconfig.real: Can't create temporary cache file /etc/ld.so.cache~: Permission denied


我注意到输出末尾的错误消息
/sbin/ldconfig.real: Can't create temporary cache file /etc/ld.so.cache~: Permission denied
我决定阅读ldconfig的手册页来了解更多关于ldconfig -v的功能。
ldconfig creates the necessary links and cache to the most recent shared libraries found in the di‐rectories specified on the command line, in the file /etc/ld.so.conf, and in the trusted directories, /lib and /usr/lib

-v, --verbose
              Verbose mode.  Print current version number, the name of each directory as it is scanned, and
              any links that are created.  Overrides quiet mode.


由于ldconfig -v flag与ldconfig做的是相同的事情,但只是有更详细的输出,我决定不使用它来运行ldconfig

$ ldconfig
/sbin/ldconfig.real: Can't create temporary cache file /etc/ld.so.cache~: Permission denied


这一次,之前的错误信息是唯一的输出。在使用sudo之前,我想了解更多关于发生了什么。由于ldconfig试图创建一个名为/etc/ld.so.cache~的文件,我决定查找该文件或具有类似名称的文件。

$ find /etc -name 'ld.so*'
/etc/ld.so.cache
find: ‘/etc/ssl/private’: Permission denied
find: ‘/etc/softhsm’: Permission denied
/etc/ld.so.conf.d
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
/etc/ld.so.conf


我想/etc/ld.so.cache一定是ldconfig手册页所说的该高速缓存。我决定在那里查找我的库名称。
$ grep libhttpserver /etc/ld.so.cache
但是grep没有返回匹配项,我转而查找/usr/local/lib

$ grep /usr/local/lib /etc/ld.so.cache
grep: /etc/ld.so.cache: binary file matches


在阅读ldconfig的手册页时,我了解到-p标志以人类可读的格式打印该高速缓存,这是一个二进制文件,所以我在ldconfig -p的输出中搜索/usr/local/lib
ldconfig -p | grep /usr/local/lib
我在/usr/local/lib中找到了其他库的匹配项(例如libwayland,但没有libhttpserver

libwayland-server.so.0 (libc6,x86-64) => /usr/local/lib/x86_64-linux-gnu/libwayland-server.so.0
    libwayland-server.so (libc6,x86-64) => /usr/local/lib/x86_64-linux-gnu/libwayland-server.so
    libwayland-egl.so.1 (libc6,x86-64) => /usr/local/lib/x86_64-linux-gnu/libwayland-egl.so.1
    libwayland-egl.so (libc6,x86-64) => /usr/local/lib/x86_64-linux-gnu/libwayland-egl.so
    libwayland-cursor.so.0 (libc6,x86-64) => /usr/local/lib/x86_64-linux-gnu/libwayland-cursor.so.0
    libwayland-cursor.so (libc6,x86-64) => /usr/local/lib/x86_64-linux-gnu/libwayland-cursor.so
    libwayland-client.so.0 (libc6,x86-64) => /usr/local/lib/x86_64-linux-gnu/libwayland-client.so.0
    libwayland-client.so (libc6,x86-64) => /usr/local/lib/x86_64-linux-gnu/libwayland-client.so

...


很明显,链接器ld.so曾经能够找到位于/usr/local/lib中的库,因为它们在链接器的缓存中。我通过检查/etc/ld.so.conf/etc/ld.so.conf.d证实了这一点。
$ cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf
$ grep -x /usr/local/lib /etc/ld.so.conf.d/*.conf
/etc/ld.so.conf.d/libc.conf:/usr/local/lib
我很好奇在我的系统上运行ldconfig通常是什么,但是我找不到一个systemd服务,crontab,或者init.d脚本来运行它。在安装到/usr/local/lib之前,我已经从源代码构建了库,但是我从来没有遇到过这个问题。
我终于在我不久前安装的一个这样的库的源代码中的构建脚本中找到了对ldconfig的引用。我得出的结论是,安装后是否运行ldconfig取决于源代码的作者,而系统是否在启动时自动运行ldconfig等取决于您使用的是什么Linux发行版。
在说服自己重新构建链接器缓存不会造成任何伤害之后,我最终以root身份运行ldconfig
sudo ldconfig
这一次,没有关于创建临时缓存文件失败的错误消息。在尝试再次运行代码之前,我确认链接器缓存现在包含对libhttpserver的引用。

$ ldconfig -p|grep libhttpserver
    libhttpserver.so.0 (libc6,x86-64) => /usr/local/lib/libhttpserver.so.0
    libhttpserver.so (libc6,x86-64) => /usr/local/lib/libhttpserver.so


最后,我再次尝试运行程序,并取得了成功。
$ ./server
请注意,在运行sudo ldconfig之后,我不需要重新编译我的程序,因为它已经包含了对正确共享库的引用。问题只是在安装libhttpserver之后,需要通过运行sudo ldconfig来更新链接器缓存/etc/ld.so.cache
当您从源代码构建库时,您可能会遇到这个问题,这取决于源代码维护者是否选择在其安装脚本中包含该步骤。

相关问题