ldconfig
是 Linux 系统中用于配置动态链接器运行时绑定的重要工具。它主要处理共享库的缓存和链接配置。
/etc/ld.so.conf
中指定的目录,创建共享库的缓存文件 /etc/ld.so.cache
选项 | 描述 |
---|---|
-v 或 --verbose |
详细模式,显示扫描的目录和创建的链接 |
-n |
只处理命令行指定的目录,不处理默认目录和 /etc/ld.so.conf |
-N |
不重建缓存文件 |
-X |
不更新符号链接 |
-f conf |
使用指定文件代替 /etc/ld.so.conf |
-C cache |
使用指定文件代替 /etc/ld.so.cache |
-r root |
更改根目录(chroot) |
-l |
库模式,手动链接单个库 |
-p 或 --print-cache |
打印当前缓存内容 |
-c fmt 或 --format=fmt |
指定缓存格式(旧版、新版或两者) |
-V |
显示版本信息 |
-? 或 --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
sudo ldconfig -v
/etc/ld.so.conf
- 包含要搜索共享库的目录列表/etc/ld.so.cache
- 包含已找到库的缓存/etc/ld.so.conf.d/*.conf
- 许多发行版使用此目录存放额外的配置文件ldconfig
ldconfig
ldconfig
LD_LIBRARY_PATH
环境变量可以临时指定库路径,但不推荐生产环境使用如果遇到库找不到的问题:
/etc/ld.so.conf
指定的路径中ldconfig
更新缓存ldd
检查程序的库依赖关系strace
跟踪库加载过程ldd /path/to/program
strace /path/to/program 2>&1 | grep open
通过合理使用 ldconfig
,可以确保系统正确找到和使用共享库,是 Linux 系统维护的重要工具之一。