error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file

在做共享单车时,遇到动态库无法连接的问题

  • 问题查找
  • 临时解决(只能暂时解决,如果关掉终端需要重新配置)
  • (进阶)永久解决
  • 参考文献

./shared_bike3.out: error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file or directory

问题查找

ldd ./shared_bike3.out 
linux-vdso.so.1 (0x00007ffed0140000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fd00491f000)
libevent-2.1.so.7 => /usr/local/lib/libevent-2.1.so.7 (0x00007fd0046c9000)
libprotobuf.so.16 => /usr/local/lib/libprotobuf.so.16 (0x00007fd004240000)
liblog4cpp.so.5 => not found
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fd003eb7000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fd003c9f000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd0038ae000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd004d7d000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fd003691000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd0032f3000)

error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file_第1张图片

我们可以看到 liblog4cpp.so.5找不到

安装locate命令

sudo apt install mlocate

依次定位缺乏的库

locate liblog4cpp.so.5

error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file_第2张图片

wxncom@wxncom-virtual-machine:~/projects/shared_bike3/bin/x64/Debug$ locate liblog4cpp.so.5
/home/wxncom/projects/third/lib/log4cpp/liblog4cpp.so.5
/home/wxncom/projects/third/lib/log4cpp/liblog4cpp.so.5.0.6
/home/wxncom/shared_bike/git/log4cpp/src/.libs/liblog4cpp.so.5
/home/wxncom/shared_bike/git/log4cpp/src/.libs/liblog4cpp.so.5.0.6
/home/wxncom/shared_bike/third/lib/log4cpp/liblog4cpp.so.5
/home/wxncom/shared_bike/third/lib/log4cpp/liblog4cpp.so.5.0.6

临时解决(只能暂时解决,如果关掉终端需要重新配置)

路径找到了,那我们就添加路径
/home/wxncom/projects/third/lib/log4cpp/

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/wxncom/projects/third/lib/log4cpp/

此时,我们再去使用ldd shared_bike3.out查看库是否完整
error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file_第3张图片
此时我们运行项目./shared_bike3.out /home/wxncom/projects/conf/shared_bike.ini /home/wxncom/projects/conf/log.conf

error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file_第4张图片
开心,运行成功!

(进阶)永久解决

输入命令cd,进入用户目录下。

cd

再运行ll命令

ll

error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file_第5张图片
输入命令vim ./.bashrc,编辑.bashrc文件。

vim ./.bashrc

然后在当前模式(命令模式)下输入shift+g(也就是G)移动到这个文件的最后一行,然后输入i,进入输入模式。
error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file_第6张图片
在这里输入配置环境变量的命令

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/wxncom/projects/third/lib/log4cpp/

error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file_第7张图片
然后按一下esc见进入命令模式,输入命令:wq,按回车,保存并退出。

接着输入如下命令,使修改生效。

source /etc/profile

error while loading shared libraries: liblog4cpp.so.5: cannot open shared object file: No such file_第8张图片

参考文献

2023框框

你可能感兴趣的:(共享单车,git,linux,ubuntu)