3

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.

Simon
  • 2,067
  • 2
  • 17
  • 30
  • 6
    That is an error thrown by the linker, not by the compiler. You forgot to link a library you included. – arkascha Jan 29 '14 at 15:41
  • @arkascha - you are right; I had already removed my comment since it was irrelevant. – Floris Jan 29 '14 at 15:44
  • 1
    I am assuming `leveldb` is from http://code.google.com/p/leveldb/ Did you download and install `leveldb` correctly? You may need to provide the path to the library in your linker command. For that matter - you may need to build the library first, depending on how you downloaded it. – Floris Jan 29 '14 at 15:49
  • 1
    @Floris yes I actually did forget to build it ... that works better when it is ! – Simon Jan 29 '14 at 15:58

2 Answers2

5

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.

ismail
  • 46,010
  • 9
  • 86
  • 95
  • I got : /usr/bin/ld: cannot find -lleveldb – Simon Jan 29 '14 at 15:45
  • Thats because the library is not in /usr/lib or /usr/local/lib, updating answer accordingly. – ismail Jan 29 '14 at 15:46
  • You might need a -L too to specify a path for where to find the leveldb library. That goes in the link-phase though, where you set the target. The above looks like a compile command. – CashCow Jan 29 '14 at 15:46
  • @CashCow: `g++` will compile and link, unless you tell it otherwise. – Mike Seymour Jan 29 '14 at 16:24
  • @ismail why is it that if I put the source file after the shared lib I get the same errors but it works vice versa. Like so `testLevelDB.cpp -lleveldb` works but `-lleveldb testLevelDB.cpp` breaks – Peter Chaula Dec 05 '16 at 06:50
  • 1
    @Laser because linking order matters. – ismail Dec 05 '16 at 14:30
  • Thanks. It's a bit confusing since order doesn't matter in most Gnu programs – Peter Chaula Dec 05 '16 at 18:52
  • 1
    @Laser linking order is quite important, just read about another interesting bug on Windows, due to linking order (partially): https://connect.microsoft.com/VisualStudio/feedback/details/3103806 – ismail Dec 06 '16 at 07:42
1

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

GabiMe
  • 18,105
  • 28
  • 76
  • 113
  • Hu, no. How do I link it ? I specify the folder of the headers ? – Simon Jan 29 '14 at 15:43
  • Depends what "leveldb" is.. Is it a library (ends with .so) - use -l.. or if is it a cpp file or .a file - just add them to the gcc input – GabiMe Jan 29 '14 at 15:47