ldconfig:安装 .so 后刷新共享库缓存

重建 /etc/ld.so.cache 让动态链接器找到新安装的库,并用 ldconfig -p 检查缓存。

Linux链接器共享库路径中风险
添加库文件(或修改 /etc/ld.so.conf.d/)后执行 sudo ldconfig,再用 ldconfig -p | grep 库名 验证。

01命令

命令
$ sudo ldconfig && ldconfig -p | grep libtorch

02作用说明

动态加载器启动时并不扫描文件系统——它读取预先生成的索引 /etc/ld.so.cache,该缓存由 ldconfig 根据 /etc/ld.so.conf.d 中的目录生成。把 .so 放进 /usr/local/lib 后不执行 ldconfig,加载器就找不到它,这正是文件明明存在却报“cannot open shared object file”的原因。ldconfig -p 可以列出全部缓存索引用于验证;LD_LIBRARY_PATH 是按用户的临时覆盖手段(适合调试,不适合生产)。

03风险

风险中风险
!注意: 不要把 LD_LIBRARY_PATH 当永久方案——它会影响所有进程的库解析;正确做法是配置 /etc/ld.so.conf.d 并运行 ldconfig。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • ldconfig(8) — Arch Linux manual
    Arch Linux · technical · 2026-09-17
    依据: Documents ldconfig rebuilding /etc/ld.so.cache and the trusted library directories
  • ldconfig(8) — Linux manual page
    man7.org · technical · 2026-09-17
    依据: ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified in /etc/ld.so.conf; -p prints the cache

最后验证: 2026-09-17 · 分类: 开发者