gpt4 book ai didi

c++ - 在 leveldb 的包装类中链接静态方法

转载 作者:搜寻专家 更新时间:2023-10-31 01:54:57 35 4
gpt4 key购买 nike

我尝试为 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 */
}

这会不会在链接时以某种方式产生问题?

最佳答案

我认为这是库链接顺序。尝试将 -leveldb 放在 CLevelDBStore.o 之后:

g++ -Wall test.cpp -O0 -ggdb -L/path/to/leveldb -I/path/to/leveldb/include -Wall -O2 ./CLevelDBStore.o -lleveldb -lz -lpthread -llog4cxx -o levelDBStoretest

来自 GCC Options for Linking :

-llibrary

Search the library named library when linking. It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o' searches libraryz' after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded.

关于c++ - 在 leveldb 的包装类中链接静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8975344/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com