解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/deeplan_1994/article/details/83927832

一、问题

运行hello程序时,用到了自己编写的动态库,在目录/usr/local/lib目录下,运行时出现

error while loading shared libraries: libhello.so.1: cannot open shared object file: No such file or directory

分析原因:链接器ld提示找不到库文件。ld默认的目录是/lib和/usr/lib,如果放在其他路径也可以,需要让ld知道库文件所在的路径。找不到这个动态库,CMakeLists.txt是这样写的:

cmake_minimum_required(VERSION 3.0)
INCLUDE_DIRECTORIES(/usr/local/include/hello)
LINK_directories("/usr/locla/lib")
ADD_EXECUTABLE(main main.c)
TARGET_LINK_LIBRARIES(main libhello.so)

这里已经指定了,可是还出现上面问题,根据错误提示找到博客:https://blog.csdn.net/yjk13703623757/article/details/53217377

解决方法如下:

方法1:

# vim /etc/ld.so.conf      //在新的一行中加入库文件所在目录
  /usr/lib  

# ldconfig                 //更新/etc/ld.so.cache文件

方法2:

1.将用户用到的库统一放到一个目录,如 /usr/loca/lib
# cp libXXX.so.X /usr/loca/lib/           

2.向库配置文件中,写入库文件所在目录
# vim /etc/ld.so.conf.d/usr-libs.conf    
  /usr/local/lib  

3.更新/etc/ld.so.cache文件
# ldconfig  

 附加:

如果共享库文件安装到了/lib或/usr/lib目录下, 那么需执行一下ldconfig命令

ldconfig命令的用途, 主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,
搜索出可共享的动态链接库(格式如lib*.so*), 进而创建出动态装入程序(ld.so)所需的连接和缓存文件. 缓存文件默认为/etc/ld.so.cache, 此文件保存已排好序的动态链接库名字列表. 

猜你喜欢

转载自blog.csdn.net/deeplan_1994/article/details/83927832