RocksDB编译运行
- 环境:CentOS 7
- C++11支持
- 直接通过
make
/make all
会生成debug模式,这会比release模式慢
环境准备
安装g++,确保g++版本足够新
yum install gcc48-c++
g++ --version
安装RocksDB所需的依赖
git
由于需要从github获取所以需要先安装git
yum install git
gflags
git clone https://github.com/gflags/gflags.git
cd gflags
./configure && make && make install
后来更新了gflags需要用cmake进行编译,修改之后的步骤:
mkdir build
cmake ..
make&&make install
snappy
yum install snappy snappy-devel
zlib
yum install zlib zlib-devel
bzip2
yum install bzip2 bzip2-devel
lz4
yum install lz4-devel
ASAN
yum install libasan
zstandard:
wget https://github.com/facebook/zstd/archive/v1.1.3.tar.gz
mv v1.1.3.tar.gz zstd-1.1.3.tar.gz
tar zxvf zstd-1.1.3.tar.gz
cd zstd-1.1.3
make && make install
RocksDB编译
编译静态库,release mode,获得librocksdb.a
make static_lib
编译动态库,获得librocksdb.so
,release mode,获得lbrocksdb.so
make shared_lib
编译获取动态库之后将其拷贝到/usr/lib中(以下操作均在root模式下机型,非root模式需要添加sudo
)
cp -r include/ /usr/include
cp librocksdb.so.5.14.0 /usr/lib
ln -s /usr/lib/librocksdb.so.5.14.0 /usr/lib/librocksdb.so.5
ln -s /usr/lib/librocksdb.so.5.14.0 /usr/lib/librocksdb.so
ld config
RocksDB简单测试程序
#include <cstdio>
#include <string>
#include "rocksdb/db.h"
#include "rocksdb/slice.h"
#include "rocksdb/options.h"
using namespace std;
using namespace rocksdb;
const std::string PATH = "/kv/rocksdb_tmp";
int main(){
DB* db;
Options options;
options.create_if_missing = true;
Status status = DB::Open(options, PATH, &db);
assert(status.ok());
Slice key("foo");
Slice value("bar");
std::string get_value;
status = db->Put(WriteOptions(), key, value);
if(status.ok()){
status = db->Get(ReadOptions(), key, &get_value);
if(status.ok()){
printf("get %s\n", get_value.c_str());
}else{
printf("get failed\n");
}
}else{
printf("put failed\n");
}
delete db;
}
编译:
g++ -std=c++11 -o rocksdbtest.cpp rocksdbtest -lpthread -lrocksdb
问题解决
对于找不到gflags的错误,需要把/usr/local/lib
添加到动态链接库里面
export LD_LIBRARY_PATH=/usr/local/lib
为了让这个设置永久生效,可以在~/.bash_profile
文件中添加设置指令
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib