您的位置:首页 > 其它

编译、测试 leveldb

2015-03-03 20:57 1601 查看

准备工作

环境:Mac os x ,Linux 都可以

下载leveldb 源代码github地址

编译 leveldb

(为了能够调试,修改 Makefile 为debug mode(B 模式)OPT ?= -g2)

cd leveldb
make


编译后会生成库文件:libleveldb.a

测试

dbtest.cpp

#include <iostream>
#include "leveldb/db.h"

using namespace std;
using namespace leveldb;

int main() {
DB *db ;
Options op;
op.create_if_missing = true;
Status s = DB::Open(op,"/tmp/testdb",&db);

if(s.ok()){
cout << "创建成功" << endl;
s = db->Put(WriteOptions(),"abcd","1234");
if(s.ok()){
cout << "插入数据成功" << endl;
string value;
s = db->Get(ReadOptions(),"abcd",&value);
if(s.ok()){
cout << "获取数据成功,value:" << value << endl;
}
else{
cout << "获取数据失败" << endl;
}
}
else{
cout << "插入数据失败" << endl;
}
}
else{
cout << "创建数据库失败" << endl;
}
delete db;
return 0;
}


编译、链接

g++ dbtest.cpp -o dbtest -L. -I./include -lpthread -lleveldb


运行结果:



原文地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息