小明思考

Just a software engineer
posts - 124, comments - 36, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

leveldb研究 - 编译/调试

Posted on 2012-03-08 11:44 小明 阅读(4785) 评论(1)  编辑  收藏 所属分类: 分布式计算
leveldb是 google对bigtable的一个简化版的开源实现,很有研究价值。

我的编译环境:ubuntu 32&g++ 4.6

1.安装git并下载代码

sudo apt-get install git-core
git clone https:
//code.google.com/p/leveldb/

2. 编译leveldb

cd leveldb
.
/build_detect_platform
make

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

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

3. 编写测试程序
ldbtest.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 
<< "create successfully" << endl;
        s 
= db->Put(WriteOptions(),"abcd","1234");
        
if(s.ok()){
            cout 
<< "put successfully" << endl;
            
string value;
            s 
= db->Get(ReadOptions(),"abcd",&value);
            
if(s.ok()){
                cout 
<< "get successfully,value:" << value << endl;
            }
            
else{
                cout 
<< "get failed" << endl;
            }
        }
        
else{
            cout 
<< "put failed" << endl;
        }
    }
    
else{
        cout 
<< "create failed" << endl;
    }
    delete db;
    
return 0;
}
注意link的时候需要加上-lpthread.

运行后得到结果:(Eclipse中运行)


评论

# re: leveldb研究 - 编译/调试  回复  更多评论   

2012-03-15 14:47 by ayanmw
给你补充下:
编译命令为g++ ldbtest.cpp -o ldbtest -L. -I./include -lpthread -lleveldb

(工作目录就是leveldb目录中) include下面是leveldb的头文件,-L 搜索library 为了libleveldb.a能够被调用。

另外,编译的时候 可以将Makefile中说的snappy 压缩 也带上,还有谷歌perfecttools ,这个可选。压缩还是有必要的吧。

只有注册用户登录后才能发表评论。


网站导航: