前回 Redis をインストールしたので今回は hiredis をインストールします。
環境
$ cat /etc/issue Ubuntu 22.04.3 LTS \n \l
インストール
ソースを持ってきます。
$ git clone https://github.com/redis/hiredis.git
make します。
$ cd hiredis/
$ make
make install します。
$ sudo make install [sudo] password for (省略): mkdir -p /usr/local/include/hiredis /usr/local/include/hiredis/adapters /usr/local/lib cp -pPR hiredis.h async.h read.h sds.h alloc.h sockcompat.h /usr/local/include/hiredis cp -pPR adapters/*.h /usr/local/include/hiredis/adapters cp -pPR libhiredis.so /usr/local/lib/libhiredis.so.1.2.1-dev cd /usr/local/lib && ln -sf libhiredis.so.1.2.1-dev libhiredis.so && ln -sf libhiredis.so.1.2.1-dev libhiredis.so.1 cp -pPR libhiredis.a /usr/local/lib mkdir -p /usr/local/lib/pkgconfig cp -pPR hiredis.pc /usr/local/lib/pkgconfig
デフォルトでは /usr/local/lib および /usr/local/include に必要なファイルが作成されます。
サンプルプログラムの実行
hiredis/example ディレクトリにある example.c を動かしてみます。
コンパイル
$ gcc example.c -I/usr/local/include/hiredis -lhiredis
実行します。
$ ./a.out ./a.out: error while loading shared libraries: libhiredis.so.1.2.1-dev: cannot open shared object file: No such file or directory
共有ライブラリがないぞと怒られました。
エラー解決
実行ファイルが利用する共有ライブラリを調べてみます。
参考:第29回 共有ライブラリと依存関係[1] | gihyo.jp
$ ldd a.out linux-vdso.so.1 (0x00007ffeb69be000) libhiredis.so.1.2.1-dev => not found libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd64cb8c000) /lib64/ld-linux-x86-64.so.2 (0x00007fd64cdc4000)
hiredisの共有ライブラリだけ not found と表示されています。
環境変数を使って共有ライブラリのパスを指定します
$ export LD_LIBRARY_PATH=/usr/local/lib
参考:Linuxで共有ライブラリの作成とダイナミックリンクをする方法: 小粋空間
もう一度 ldd コマンドで調べます。
$ ldd a.out linux-vdso.so.1 (0x00007fffb5954000) libhiredis.so.1.2.1-dev => /usr/local/lib/libhiredis.so.1.2.1-dev (0x00007f0615d8a000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0615b59000) /lib64/ld-linux-x86-64.so.2 (0x00007f0615da7000)
not foundの表示が消えました。
改めて実行する
$ ./a.out Connection error: Connection refused
共有ライブラリのエラーは消えましたが、Connection refused と表示されました。
redisが動いているか確認します。
$ redis-cli ping Could not connect to Redis at 127.0.0.1:6379: Connection refused
サーバを起動します。
$ redis-server & [1] 67300 (省略)
再度実行します。
$ ./a.out PING: PONG SET: OK SET (binary API): OK GET foo: hello world INCR counter: 1 INCR counter: 2 0) element-9 1) element-8 2) element-7 3) element-6 4) element-5 5) element-4 6) element-3 7) element-2 8) element-1 9) element-0 RPUSH reply: 10
サンプルを正常に動かすことができました。
サーバを終了させます。
$ redis-cli shutdown 67300:M 21 Sep 2023 07:51:34.970 * User requested shutdown... 67300:M 21 Sep 2023 07:51:34.970 * Saving the final RDB snapshot before exiting. 67300:M 21 Sep 2023 07:51:34.973 * DB saved on disk 67300:M 21 Sep 2023 07:51:34.973 # Redis is now ready to exit, bye bye... [1]+ Done redis-server
共有ライブラリが見つからなかったときの対処
①環境変数の設定
今回は環境変数の設定で共有ライブラリの問題を解決しましたが、毎回 export と唱えるわけにはいかないので bash_profile に書いておきます。
$ emacs ~/.bash_profile
下記のとおり記述します。
export LD_LIBRARY_PATH=/usr/local/lib
設定を反映させます。
$ source ~/.bash_profile
②confファイルの作成
/etc/ld.so.conf.d/配下に設定を書き、ldconfigを実行するという方法もあるようです。筆者の環境では下記のとおり、3つの設定ファイルがすでにありました。
$ ls /etc/ld.so.conf.d
fakeroot-x86_64-linux-gnu.conf libc.conf x86_64-linux-gnu.conf
ここにもう一つhiredis.confといった名前の設定ファイルを追加してldconfigするというのが選択肢の1つとなります。bash_profileに追記するという方法をとったので、今回はhiredis.confの作成はしていません。
参考
③LDFLAGを使う
コンパイル時にLDFLAGを指定する方法もあるようです。hiredis のコンパイル時にこの方法を使うことも可能だと思われますが、再度コンパイルするのが面倒だったので今回は試しておりません。
参考:Linux: ライブラリの動的リンクでエラーが出た場合の対処方法
まとめ
以上でC言語からredisを利用できるようになりました。