leveldb: compile error: undefined reference

I download and build leveldb according to this README, and then properly add libleveldb.a and the include/leveldb directory (header files) to my environment path.

My test code:

/*leveldb_test.cpp
 */
    
#include <iostream>
#include <sstream>
#include <string>
#include <leveldb/db.h>

using namespace std;

int main(int argc, char** argv)
{
    leveldb::DB* db;
    leveldb::Options options;
    options.create_if_missing = true;

    leveldb::Status status = leveldb::DB::Open(options, "./testdb", &db);
    assert(status.ok());

    return 0;
}

And I compile it as:

g++ leveldb_test.cpp -o leveldb_test -lpthread -lleveldb -std=c++11

It produces “undefined reference” errors:

leveldb_test.o: In function `main':
leveldb_test.cpp:(.text+0x79): undefined reference to `leveldb::DB::Open(leveldb::Options const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, leveldb::DB**)'
leveldb_test.cpp:(.text+0x29e): undefined reference to `leveldb::Status::ToString[abi:cxx11]() const'
collect2: error: ld returned 1 exit status

What is the problem?

My environment: Linux version 3.10.0-327.el7.x86_64 - gcc version 4.8.3 20140911 - Red Hat 4.8.3-9

Thanks for any advise.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 4
  • Comments: 15 (3 by maintainers)

Most upvoted comments

Try the below, library mentioned as last of argument - $ g++ getting_started.cc -lleveldb