g++ can't find headers but I did include them

时光毁灭记忆、已成空白 提交于 2020-01-02 08:15:11

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/21435690/g-cant-find-headers-but-i-did-include-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!