Question

I am starting on c++ and already going wrong ...

I am trying to compile a small test of levelDB :

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

using namespace std;

int main() {
  leveldb::DB* db;
  leveldb::Options options;
  options.create_if_missing = true;
  leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
  assert(status.ok());

  return 1;
}

Here is the g++ command :

g++ -I include/ testLevelDB.cpp

Output:

/tmp/ccuBnfE7.o: In function `main':
testLevelDB.cpp:(.text+0x14): undefined reference to `leveldb::Options::Options()'
testLevelDB.cpp:(.text+0x57): undefined reference to `leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)'

The include folder is the one with the levelDB headers.

Was it helpful?

Solution

You need to tell the linker to link to the leveldb library such as

g++ -I include/ testLevelDB.cpp -lleveldb

But this won't work if the library is not in /usr/lib or /usr/local/lib for that case assuming the libleveldb.so exists in some path called $LEVELDB_PATH you need to do

g++ -I include -L $LEVELDB_PATH testLevelDB.cpp -lleveldb

-L is much like -I but it tells the linker where to looks for libraries.

Also since you seem to be new to gcc world, please have a look at this gcc intro document.

OTHER TIPS

It is a linkage error. Not related to the headers. Did you link with this lib (-l..) ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top