Introduction
RocksDB is a high performance embedded key value storage engine which is written in C++. Though its name tells that it is a database, it is actually a C++ library providing a bunch of API instead of a Client-Server architected database.
I am not a C++ programmer and when trying to play with RocksDB following the Getting Started guide from the RocksDB website, I found it is not easy to run the example in CentOS 7 with either dynamic or static linkage. To be more specific with dynamic linkage, the search paths of dynamic libraries vary from Linux distributions but the installation script of RocksDB does not take care of that.
This post is intended for those who want to play with RocksDB but failed to run the example. What will be mentioned including
- How to install RocksDB as dynamic library from source in CentOS 7
- How to install RocksDB as static library in CentOS 7
- How to run the example program
Here is the demo program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Before you continue, please install the dependencies.
1
|
|
Dynamic Linkage
Install the shared lib
First, run the command line to build dynamic library
1
|
|
The Makefile
provides a target called install-shared
to install RocksDB shared lib.
1
|
|
The target actually includes two jobs:
- Copy all the header files under
$SRC_ROOT/include/rocksdb
to/usr/local/include/rocksdb
- Copy the
.so
file and symbolic links to/usr/local/lib
Run the example
- Compile the program with following command
1
|
|
- Run the program with
./demo
and an error occurs
1
|
|
The problem with the install-shared
target is that /usr/local/lib
is in g++
’s dynamic library search paths but not in ldconfig
’s in CentOS 7 by default. So the program can be built during compiling time but will cause dynamic library load error during run time. This can be verified by following commands:
- Show search paths of g++
1
|
|
- Show search paths of ldconfig
1
|
|
There are 2 ways to resolve the issue. And EITHER one is ok.
- Installing
.so
file and symbolic links into both the search paths ofg++
andldconfig
1 2 |
|
- Add
/usr/local/lib
to theldconfig
’s search paths.
1 2 |
|
Static Linkage
As mentioned before, /usr/local/lib
is in g++’s search path, thus there is no such dynamic load issue with static RocksDB. First build and install static RocksDB.
1 2 |
|
Then compile the demo program with
1
|
|
or simply
1
|
|
Don’t miss the pthread
lib.
Conclusion
The problem is caused by shared library search paths which could vary from different Linux distributions. A good solution to avoid the issue is packaging the files into a rpm package, and deb package in Debian platform of course.
It is worth noting that /usr/lib64
, instead of /usr/lib
, is the common place to hold the lib files in x86_64 RedHat family OSes such as CentOS, Fedora. Though /usr/lib
works but /usr/lib64
is a better place for the lib files in RedHat family. There is a patch to take care of that when creating a RPM.