hiro99ma blog

linux: 共有ライブラリの検索

2025/04/27

libsecp256k1 や libwally-core などのライブラリを使っている。
configure でオプションを指定しないと /usr/local の下にインストールするので、 しばしばビルドし直すライブラリは $HOME/.local にインストールするようにしている。
それが正しいというわけではないのだけど、sudo しなくてアクセスできた方が開発中っぽい感じがして削除しても良いかなという期分になる(個人の感想)。

共有ライブラリの検索パスに $HOME/.local/lib は入っていないので $HOME/.bashrcLD_LIBRARY_PATH を export するようにしていた。
それで $HOME/.local/lib にあるライブラリはリンクできていたのだが /usr/local/lib に置いていたライブラリを見つけられずにリンクできなかった。
なんとなく /usr/local/lib はシステムが自動的に検索するのだと思っていたのだけど、実はそうではない??

今使っているのは Raspberry Pi 4 で動いている Raspbian OS だ。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 12 (bookworm)
Release:        12
Codename:       bookworm

検索の優先順位

昨年調べたときはこうだった。
Linux 共通なのかディストリビューションで違うのかは確認していないが、なんとなくディストリビューションごとという気がしている。

  1. 環境変数LD_LIBRARY_PATH
  2. /etc/ld.so.confやそのインクルードファイルで指定されたディレクトリ
  3. /etc/ld.so.cacheにキャッシュされているパス
  4. 必要に応じて標準パス(/lib, /usr/lib, /lib64, /usr/lib64)やカレントディレクトリ

今回の原因は?

今回探すことができなかったのは secp256k1 なのだが、そういえば sudo make install しただけだった。 再起動もしていない。

もう一度 make install して出力を確認する。

......
libtool: finish: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/sbin" ldconfig -n /usr/local/lib
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/local/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the 'LD_RUN_PATH' environment variable
     during linking
   - use the '-Wl,-rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to '/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
......

メッセージを見る限り、自動で /usr/local/lib を参照してくれるというわけではないようだ。
4つ提案されていて、環境変数を操作したくないなら最後の /etc/ld.so.conf に追加する方法になる。
今は自分で admin になることができるから自由にできるが、管理者が別になっていることもあろうからどれが一番良いとかは言えないな。

/etc/ld.so.conf への追加

/etc/ld.so.conf への追加とあるが、ファイルの中身は include だけだ。

include /etc/ld.so.conf.d/*.conf

各 conf ファイルを確認しよう。

$ ls /etc/ld.so.conf.d/
aarch64-linux-gnu.conf  fakeroot-aarch64-linux-gnu.conf  libc.conf

$ cat /etc/ld.so.conf.d/aarch64-linux-gnu.conf
# Multiarch support
/usr/local/lib/aarch64-linux-gnu
/lib/aarch64-linux-gnu
/usr/lib/aarch64-linux-gnu

$ cat /etc/ld.so.conf.d/fakeroot-aarch64-linux-gnu.conf
/usr/lib/aarch64-linux-gnu/libfakeroot

$ cat /etc/ld.so.conf.d/libc.conf
# libc default configuration
/usr/local/lib

libc.conf にあるやん!

ldconfigの説明に

とあるので、ldconfig はパスがあるからといって自動的に探すわけではなく、キャッシュに登録されているからこのライブラリを使いますね、という感じなのだろう。
確かにキャッシュにはなかった。

$ ldconfig --print-cache | grep libsecp256k1
$ sudo ldconfig
$ ldconfig --print-cache | grep libsecp256k1
        libsecp256k1.so.5 (libc6,AArch64) => /usr/local/lib/libsecp256k1.so.5
        libsecp256k1.so (libc6,AArch64) => /usr/local/lib/libsecp256k1.so

これで大丈夫だ。

# 変更前
$ ./app
./app: error while loading shared libraries: libsecp256k1.so.5: cannot open shared object file: No such file or directory

# 変更後
$ ./app
(期待した動作)

 < Top page


コメント(Google Formへ飛びます)

GitHub

X/Twitter

Homepage