3

我开始使用 c++ 并且已经出错了......

我正在尝试编译 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;
}

这是 g++ 命令:

g++ -I include/ testLevelDB.cpp

输出:

/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**)'

包含文件夹是带有 levelDB 标头的文件夹。

4

2 回答 2

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.

于 2014-01-29T15:44:02.237 回答
1

这是一个链接错误。与标题无关。您是否与此库 (-l..) 链接?

于 2014-01-29T15:43:05.200 回答