若文中图片无法加载,请访问图片备份地址
多次安装都遇到问题且总是忘记解决方法,因此记录下来
1. 安装过程
1.1 环境依赖
sudo apt-get install gcc g++ libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev libgflags-dev
1.2 下载源码
下载release的最新版,我这里是v6.16.4
tar -xzvf rocksdb-6.16.4.tar.gz
cd rocksdb-6.16.4/
1.3 编译安装
make static_lib
// 编译静态库
make shared_lib
// 编译动态库
注意!!!
如果出现下述错误:
// 错误示例
# make shared_lib
$DEBUG_LEVEL is 0
GEN util/build_version.cc
CCLD librocksdb.so.6.13.0
/usr/bin/ld: ./memory/concurrent_arena.o: relocation R_X86_64_TPOFF32 against symbol _ZN7rocksdb15ConcurrentArena9tls_cpuidE' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: ./monitoring/iostats_context.o: relocation R_X86_64_TPOFF32 against symbol _ZN7rocksdb15iostats_contextE' can not be used when making a shared object; recompile with -fPIC
解决方法:
make clean
make shared_lib
make static_lib
以上编译后会得到以下几个文件:
librocksdb.a
librocksdb.so
librocksdb.so.6
librocksdb.so.6.16
librocksdb.so.6.16.4
(第一个为静态编译得到的,后四个为动态编译得到的)
sudo make install
1.4 环境配置
sudo cp librocksdb.so.6.16.4 /usr/local/lib
// 将动态库文件复制到系统库目录下
sudo ln -s librocksdb.so.6.16.4 /usr/local/lib/librocksdb.so.6.16
sudo ln -s librocksdb.so.6.16.4 /usr/local/lib/librocksdb.so.6
sudo ln -s librocksdb.so.6.16.4 /usr/local/lib/librocksdb.so
// 链接其他动态库
修改环境变量文件
sudo vi /etc/profile
在其中添加LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
和export LD_LIBRARY_PATH
如下图所示:
至此完成安装!
2. 验证安装
2.1 终端验证
此部分参考https://www.jianshu.com/p/e54fadd52fe2
测试用例如下:
#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 = "/home/suu/Documents/rocksdbtemp"; //rocksDB的数据存储目录绝对路径
int main(){
DB* db;
Options options;
options.create_if_missing = true;
Status status = DB::Open(options, PATH, &db);
assert(status.ok());
Slice key("test01");
Slice value("success");
std::string get_value;
status = db->Put(WriteOptions(), key, value);
if(status.ok()){
status = db->Get(ReadOptions(), key, &get_value);
if(status.ok()){
printf("value is %s\n", get_value.c_str());
}else{
printf("get failed\n");
}
}else{
printf("put failed\n");
}
delete db;
}
编译并执行
g++ -std=c++11 -o test test.cxx -I /home/suu/Documents/rocksdb-6.16.4/include -lrocksdb -ldl
./test
显示value is success
则表示测试通过,安装成功!
2.2 VScode验证
如果像笔者一样使用VScode作为编辑器,则需要对VScode做一些配置才能正常使用
- 修改
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
// 添加rocksdb的头文件路径 否则编辑器会报找不到头文件的错误
"/home/suu/Documents/rocksdb-6.16.4/include",
"/home/suu/Documents/rocksdb-6.16.4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
- 修改
tasks.json
的args
部分
// -I 链入第三方include库
// -L 链入第三方lib库
// 分号前是编译 分号后是运行
"args": [
"-g",
"${file}",
"-I'/home/suu/Documents/rocksdb-6.16.4/include'",
//"-L'~/Documents/rocksdb-6.16.4'",
"-lrocksdb","-ldl",
"-o",
"${workspaceRoot}/CXX_OUT/${fileBasenameNoExtension}",
";",
"${workspaceRoot}/CXX_OUT/${fileBasenameNoExtension}"
]
此时则可以直接F5
编译运行。