0

I have install leveldb in my home directory ~/local like this.

[~/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/

My c++ program like this:

#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; 
}

I compile this C++ program like this:

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

But I get the error like this:

/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

I don't know what's wrong.

I am new to Linux. Thank you very much!

kli_nlpr
  • 894
  • 2
  • 11
  • 25
  • 1
    You don't need root to compile something in your own home directory. – Marc B Feb 09 '15 at 14:22
  • 1
    You are missing some `-I` ... flags in your compilation command. Perhaps `-I ~/local/include` ... Try compiling with `g++ -Wall -H -I ~/local/include/` – Basile Starynkevitch Feb 09 '15 at 14:22
  • Is `leveldb/iterator.h` the relative path to your file ? – Aleph0 Feb 09 '15 at 14:23
  • cp -av include/leveldb $HOME/local/include/leveldb would solve the issue – willll Feb 09 '15 at 14:32
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – sashoalm Feb 09 '15 at 15:10

2 Answers2

0

libleveldb.a misses Snappy when being linked which would be probably in libsnappy.a in the same directory.

StenSoft
  • 9,369
  • 25
  • 30
  • Thank you very much. I compile it like this. `g++ -o Main Main.cpp ~/local/lib/libleveldb.a ~/local/lib/libsnappy.a -lpthread -I ~/local/include/ `. Everything is ok. – kli_nlpr Feb 09 '15 at 15:34
0

Looks like the Makefile is incomplete.

With the current install, you need to edit the Makefile to link against snappy and to include -L/usr/local/lib instead of -L/usr/local/include.

(Will post pull request later)