简体   繁体   English

g ++找不到标头,但我确实包含了标头

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

I am starting on c++ and already going wrong ... 我从C ++开始,已经出错了...

I am trying to compile a small test of levelDB : 我正在尝试对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 ++命令:

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. include文件夹是带有levelDB标头的文件夹。

You need to tell the linker to link to the leveldb library such as 您需要告诉链接器链接到leveldb库,例如

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 但是,如果在这种情况下该库不在/usr/lib/usr/local/lib ,则这将不起作用,假设libleveldb.so存在于名为$LEVELDB_PATH某个路径中,则需要执行此操作

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

-L is much like -I but it tells the linker where to looks for libraries. -L很像-I但是它告诉链接器在哪里寻找库。

Also since you seem to be new to gcc world, please have a look at this gcc intro document. 另外,由于您似乎不熟悉gcc世界,因此请查看 gcc简介文档。

It is a linkage error. 这是一个链接错误。 Not related to the headers. 与标题无关。 Did you link with this lib (-l..) ? 您是否与此lib(-l ..)链接?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM