我尝试为 leveldb 编写一个包装类。基本上产生我的问题的头文件部分是(CLevelDBStore.h
:)
#include "leveldb/db.h"
#include "leveldb/comparator.h"
using namespace leveldb;
class CLevelDBStore {
public:
CLevelDBStore(const char* dbFileName);
virtual ~CLevelDBStore();
/* more stuff */ 67 private:
private:
CLevelDBStore();
static leveldb::DB* ldb_;
};
文件中对应的代码CLevelDBStore.cpp
为:
#include "CLevelDBStore.h"
DB* CLevelDBStore::ldb_;
CLevelDBStore::CLevelDBStore(const char* dbFileName) {
Options options;
options.create_if_missing = true;
DB::Open((const Options&)options, (const std::string&) dbFileName, (DB**)&ldb_);
Status status = DB::Open(options, dbFileName);
}
我现在尝试编译我的测试文件(test.cpp
),基本上是
#include "leveldb/db.h"
#include "leveldb/comparator.h"
#include "CLevelDBStore.h"
int main() {
std::cout << "does not compile" << std::endl;
return 0;
}
请注意,我什至还没有使用包装类。只是为了产生编译错误。
汇编
g++ -Wall -O0 -ggdb -c CLevelDBStore.cpp -I/path/to/leveldb/include
g++ -Wall test.cpp -O0 -ggdb -L/path/to/leveldb -I/path/to/leveldb/include \
-lleveldb -Wall -O2 -lz -lpthread ./CLevelDBStore.o -llog4cxx \
-o levelDBStoretest
产量
CLevelDBStore.cpp:27: undefined reference to `leveldb::DB::Open(leveldb::Options const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, leveldb::DB**)'
我查看了定义 leveldb::DB::Open 的 leveldb 代码,结果发现它是一个静态方法。
class DB {
public:
static Status Open(const Options& options,
const std::string& name,
DB** dbptr);
/* much more stuff */
}
这会在链接时以某种方式产生问题吗?