Ubuntu下安装leveldb库
一、下载Level
git clone https://gitclone.com/github.com/google/leveldb
二、下载编译leveldb需要的googletest、benchmark
- 1、进入下载好的leveldb
cd leveldb
- 2、进入third_party目录
cd third_party
- 3、下载
git clone https://gitclone.com/github.com/google/googletest
git clone https://gitclone.com/github.com/google/benchmark
三、编译安装
回到leveldb目录
mkdir build && cd build
cmake ..
cmake --build .
四、头文件、库编译文件
- 将leveldb头文件放到/usr/local/include下
sudo cp -r include/leveldb/ /usr/local/include/
- 将编译好的库文件放到/usr/local/lib下
sudo cp build/libleveldb.a /usr/local/lib/
刷新环境
sudo ldconfig
五、c++测试小例子
#include <assert.h>
#include <string.h>
#include <leveldb/db.h>
#include <iostream>
using namespace leveldb;
int main(){
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
// 打开一个数据库,不存在就创建
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());
// 插入一个键值对
status = db->Put(leveldb::WriteOptions(), "hello", "LevelDB");
assert(status.ok());
// 读取键值对
std::string value;
status = db->Get(leveldb::ReadOptions(), "hello", &value);
assert(status.ok());
std::cout << value << std::endl;
delete db;
return 0;
}
g++ hello.cc -o hello -L. -I./include -lpthread -lleveldb
./hello
打印输出
hello