執行二進位程式時出現 error while loading shared libraries: xxx.so.0: cannot open shared object file: No such file or directory,表示動態連結器 ld.so 在預設搜尋路徑(/lib、/usr/lib、/etc/ld.so.conf 所列目錄)找不到所需的共享庫。
# 常見第三方庫安裝位置
find /usr/local -name "xxx.so*"
find /opt -name "xxx.so*"
# 或使用 locate(需 updatedb)
locate xxx.so
/etc/ld.so.conf(或在 /etc/ld.so.conf.d/ 新增 custom.conf),新增庫所在目錄(如 /usr/local/lib):/usr/local/lib
sudo ldconfig -v
-v 會顯示掃描到的庫與建立的快取連結。
# 單次執行指定庫路徑
LD_LIBRARY_PATH=/usr/local/lib ./your_program
# 或寫入啟動腳本
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
ldd ./your_program
# 輸出範例
linux-vdso.so.1 => (0x00007ffc12345000)
xxx.so.0 => /usr/local/lib/xxx.so.0 (0x00007f1234567000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1234568000)
# 若顯示 "not found" 即為缺少該庫

將第三方庫目錄(通常為 /usr/local/lib)加入 /etc/ld.so.conf 並執行 ldconfig,即可永久解決 error while loading shared libraries 問題。