leveldb研究 - 编译/调试

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中运行)

你可能感兴趣的:(leveldb研究 - 编译/调试)