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标头的文件夹。
2 个解决方案
#1
5
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介绍文档。
#2
1
It is a linkage error. Not related to the headers. Did you link with this lib (-l..) ?
这是一个连锁错误。与标题无关。你有没有链接这个lib(-l ..)?
#1
5
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介绍文档。
#2
1
It is a linkage error. Not related to the headers. Did you link with this lib (-l..) ?
这是一个连锁错误。与标题无关。你有没有链接这个lib(-l ..)?