ldconfig
是 Linux 系统中用于配置动态链接器运行时绑定的重要工具。它主要处理共享库的缓存和链接配置。
/etc/ld.so.conf
中指定的目录和信任的库目录(如 /lib
和 /usr/lib
),然后更新 /etc/ld.so.cache
文件选项 | 描述 |
---|---|
-p 或 --print-cache |
打印当前缓存内容 |
-v 或 --verbose |
详细模式,显示扫描的目录和处理过程 |
-n |
只处理命令行指定的目录,不处理标准目录和配置文件 |
-N |
不重建缓存,只更新符号链接 |
-X |
不更新符号链接,只重建缓存 |
-f <conf> |
使用指定的配置文件而非 /etc/ld.so.conf |
-C <cache> |
使用指定的缓存文件而非 /etc/ld.so.cache |
-r <root> |
使用指定的根目录 |
-l |
库模式,手动链接单个库(专家模式) |
-i |
忽略辅助缓存文件 |
--help |
显示帮助信息 |
sudo ldconfig
ldconfig -p
# 1. 将路径添加到配置文件
echo "/usr/local/lib" | sudo tee -a /etc/ld.so.conf
# 2. 更新缓存
sudo ldconfig
sudo ldconfig /path/to/new/libs
ldconfig
主要使用以下配置文件:
- /etc/ld.so.conf
- 主配置文件,包含要扫描的目录列表
- /etc/ld.so.conf.d/*.conf
- 额外的配置文件目录
- /etc/ld.so.cache
- 生成的缓存文件
/etc/ld.so.conf
和 /etc/ld.so.conf.d/
中指定的目录.so
文件)libfoo.so.1
)libfoo.so -> libfoo.so.1
)/etc/ld.so.cache
二进制文件# 检查库是否在标准路径或配置路径中
ldconfig -p | grep library_name
# 如果不在,添加路径并更新
sudo ldconfig
# 删除旧缓存并重建
sudo rm /etc/ld.so.cache
sudo ldconfig
sudo ldconfig -v
ldconfig
/etc/ld.so.conf.d/
下的单独文件中-v
选项查看详细处理过程ldconfig
使更改生效ldconfig
是维护 Linux 系统共享库环境的关键工具,正确使用可以避免许多"库未找到"的问题。