解决 Linux 报错 error while loading shared libraries 动态链接库缺失修复
排查 Linux 运行程序提示 error while loading shared libraries: libssl.so.1.1: cannot open shared object file,配置 ldconfig 与 LD_LIBRARY_PATH 路径。
问题现象
在终端执行新安装的软件或自行编译的 C/C++/Go 二进制程序时程序直接退出:
text ./my_program: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory
触发根因
Linux 动态链接器(ld-linux.so)在程序启动时会沿默认系统路径(/lib、/usr/lib)检索程序依赖的动态共享对象(.so 文件)。如果目标 .so 文件被安装在自定义路径(如 /usr/local/lib 或应用私有目录)、或者系统中安装的库版本与编译时指定的 SONAME 不一致,动态加载器无法找到该库。
解决步骤
1. 使用 ldd 命令打印所有缺失的依赖符号
在终端查看程序完整的动态链接依赖拓扑:
bash ldd ./my_program
查找输出中标记为 not found 的具体动态库名称。
2. 将库所在目录注册进系统动态链接器缓存
在 /etc/ld.so.conf.d/ 下新建配置文件:
bash echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/custom_libs.conf
执行 ldconfig 刷新系统链接器高速二进制缓存:
bash sudo ldconfig
3. 临时通过环境变量指定搜索路径
在当前 Shell 会话中验证:
bash export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH ./my_program
验证确认
再次执行 ldd ./my_program:
所有 .so 库均指向具体的物理绝对路径,程序顺利拉起运行。


