编译linux中的leveldb c ++程序错误?

时间:2015-02-09 14:19:37

标签: c++ linux database leveldb

我已经在我的主目录~/local中安装了leveldb。

[~/temp/leveldb-1.15.0] $ make
[~/temp/leveldb-1.15.0] $ cp -av libleveldb.* $HOME/local/lib/
[~/temp/leveldb-1.15.0] $ cp -av include/leveldb $HOME/local/include/

我的c ++程序是这样的:

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

using namespace std; 

int main(int argc,char * argv[]) 
{ 
leveldb::DB* db; 
leveldb::Options options; 
options.create_if_missing = true; 
std::string dbpath = "tdb"; 
leveldb::Status status = leveldb::DB::Open(options, dbpath, &db); 
assert(status.ok()); 
std::string key1 = "grz"; 
std::string key2 = "grz-rt@63.com"; 
cout<<"Open db OK"<<std::endl; 

std::string value; 
leveldb::Status s ; 
s = db->Put(leveldb::WriteOptions(), key1, key2);/*key1和key2作为一对key-value对插入*/ 
s = db->Get(leveldb::ReadOptions(), key1, &value);/*根据key返回对应的value值*/ 

cout<<value<<std::endl; 
delete db;/*删除数据库*/ 

return 0; 
}

我像这样编译这个C ++程序:

g++ -o Main Main.cpp ~/local/lib/libleveldb.a -lpthread -I ~/local/include/

但我得到的错误是这样的:

/public/home/kli/local/lib/libleveldb.a(table_builder.o): In function `leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*)':
table_builder.cc:(.text+0x678): undefined reference to `snappy::MaxCompressedLength(unsigned long)'
table_builder.cc:(.text+0x6b2): undefined reference to `snappy::RawCompress(char const*, unsigned long, char*, unsigned long*)'
/public/home/kli/local/lib/libleveldb.a(format.o): In function `leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*)':
format.cc:(.text+0x5de): undefined reference to `snappy::GetUncompressedLength(char const*, unsigned long, unsigned long*)'
format.cc:(.text+0x64e): undefined reference to `snappy::RawUncompress(char const*, unsigned long, char*)'
collect2: ld returned 1 exit status

我不知道出了什么问题。

我是Linux新手。非常感谢你!

2 个答案:

答案 0 :(得分:0)

libleveldb.a在链接时错过了Snappy,它可能位于同一目录中的libsnappy.a

答案 1 :(得分:0)

看起来Makefile不完整。

使用当前安装,您需要编辑Makefile以链接snappy并包含-L/usr/local/lib而不是-L/usr/local/include

(稍后会发布拉动请求)