Linux:cannot open shared object file: No such file or directory

先说答案:执行 sudo ldconfig 或者执行 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/library。

解决思路

1. 确定相关 so 库已经在 /usr/lib/ 目录下。

2. 确定 CMakeLists.txt 中设置正确。

find_library(OSS_LIBRARY NAMES oss_c_sdk PATHS /usr/lib)
find_library(MXML_LIBRARY NAMES mxml PATHS /usr/lib)

message(STATUS "oss lib:${OSS_LIBRARY}")
message(STATUS "mxml lib:${MXML_LIBRARY}")

add_executable(...)

target_link_libraries(${PROJECT_NAME} ${OSS_LIBRARY})
target_link_libraries(${PROJECT_NAME} ${MXML_LIBRARY})

3. 确定编译通过,生成可执行文件。

[root@a7381ab12e62 build]# cmake ..
-- oss lib:/usr/lib/liboss_c_sdk.so
-- mxml lib:/usr/lib/libmxml.so
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/src/test/build

[root@a7381ab12e62 build]# make
[ 20%] ...
[ 40%] ...
[ 60%] ...
[ 80%] ...
[100%] Built target oss_demo

4. 执行。

[root@a7381ab12e62 build]# ./oss_demo 
./test_demo: error while loading shared libraries: liboss_c_sdk.so.3.0.0: cannot open shared object file: No such file or directory

5.  使用 ldd 命令查看可执行文件需要的 so 库。

[root@a7381ab12e62 build]# ldd oss_demo 
	linux-vdso.so.1 =>  (0x00007ffe1a7cb000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f7a251a0000)
	liboss_c_sdk.so.3.0.0 => not found
	libmxml.so.1 => not found
        ...

可以看到,oss_c_sdk 和 mxml 的 so 库没有找到。

6. 问题原因。

因为 oss_c_sdk 和 mxml 的 so 库是使用源码方式进行安装的。但是系统的软件包管理器(package manager)并不知道你已经安装了新库,因此使用 ldd 命令时会显示 「not found」。(如果使用 apt-get 或 yum 安装软件时,一般会安装两个包:software 和 software-dev。其中 software-dev 中包含该软件的头文件和其库文件。)

7. 解决问题。

既然系统不知道我已经安装了新的动态库,那么就应该想办法告诉系统。

1. 使用 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).

[root@a7381ab12e62 build]# ldconfig

2. 设置 LD_LIBRARY_PATH

[root@a7381ab12e62 build]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/

3. 修改 /etc/ld.so.conf 文件内容,将相关库的路径添加到 /etc/ld.so.conf 文件中

#文件 /etc/ld.so.conf
/usr/lib/

注:如果库文件放在了自定义目录下(非 /usr/lib、/lib),使用 ldconfig 命令没有作用时就使用方法2 和 方法3。

  • 9
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值