第一个 leveldb c++ 示例代码未能 link:指示的错误是什么以及如何修复它?
First leveldb c++ sample code failed to link: what's the error indicating and how to fix it?
我刚刚用 brew install leveldb
在我的 mac 上安装了 level db,并且我有这个示例代码:
#include <assert.h>
#include <string.h>
#include <leveldb/db.h>
#include <iostream>
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());
//write key1,value1
std::string key="key";
std::string value = "value";
status = db->Put(leveldb::WriteOptions(), key,value);
assert(status.ok());
status = db->Get(leveldb::ReadOptions(), key, &value);
assert(status.ok());
std::cout<<value<<std::endl;
std::string key2 = "key2";
//move the value under key to key2
status = db->Put(leveldb::WriteOptions(),key2,value);
assert(status.ok());
status = db->Delete(leveldb::WriteOptions(), key);
assert(status.ok());
status = db->Get(leveldb::ReadOptions(),key2, &value);
assert(status.ok());
std::cout<<key2<<"==="<<value<<std::endl;
status = db->Get(leveldb::ReadOptions(),key, &value);
if(!status.ok()) std::cerr<<key<<" "<<status.ToString()<<std::endl;
else std::cout<<key<<"==="<<value<<std::endl;
delete db;
return 0;
}
我尝试编译并 link 它:
$ ls /usr/local/lib/libleveldb.a
/usr/local/lib/libleveldb.a
$ clang++ -o test test.cpp /usr/local/lib/libleveldb.a -lpthread -Iinclude
Undefined symbols for architecture x86_64:
"snappy::RawCompress(char const*, unsigned long, char*, unsigned long*)", referenced from:
leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*) in libleveldb.a(table_builder.o)
"snappy::RawUncompress(char const*, unsigned long, char*)", referenced from:
leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*) in libleveldb.a(format.o)
"snappy::MaxCompressedLength(unsigned long)", referenced from:
leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*) in libleveldb.a(table_builder.o)
"snappy::GetUncompressedLength(char const*, unsigned long, unsigned long*)", referenced from:
leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*) in libleveldb.a(format.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
是不是我安装编译有问题?
看来您还需要 linking snappy。我认为这是因为 leveldb 也在使用该库。因此,您需要下载它并将其安装为静态库或共享库,并使用 -lsnappy
link。确保将 linker 标志放在 leveldb 的 linker 标志之后,以便它可以填充 leveldb 缺少的符号。
我刚刚用 brew install leveldb
在我的 mac 上安装了 level db,并且我有这个示例代码:
#include <assert.h>
#include <string.h>
#include <leveldb/db.h>
#include <iostream>
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());
//write key1,value1
std::string key="key";
std::string value = "value";
status = db->Put(leveldb::WriteOptions(), key,value);
assert(status.ok());
status = db->Get(leveldb::ReadOptions(), key, &value);
assert(status.ok());
std::cout<<value<<std::endl;
std::string key2 = "key2";
//move the value under key to key2
status = db->Put(leveldb::WriteOptions(),key2,value);
assert(status.ok());
status = db->Delete(leveldb::WriteOptions(), key);
assert(status.ok());
status = db->Get(leveldb::ReadOptions(),key2, &value);
assert(status.ok());
std::cout<<key2<<"==="<<value<<std::endl;
status = db->Get(leveldb::ReadOptions(),key, &value);
if(!status.ok()) std::cerr<<key<<" "<<status.ToString()<<std::endl;
else std::cout<<key<<"==="<<value<<std::endl;
delete db;
return 0;
}
我尝试编译并 link 它:
$ ls /usr/local/lib/libleveldb.a
/usr/local/lib/libleveldb.a
$ clang++ -o test test.cpp /usr/local/lib/libleveldb.a -lpthread -Iinclude
Undefined symbols for architecture x86_64:
"snappy::RawCompress(char const*, unsigned long, char*, unsigned long*)", referenced from:
leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*) in libleveldb.a(table_builder.o)
"snappy::RawUncompress(char const*, unsigned long, char*)", referenced from:
leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*) in libleveldb.a(format.o)
"snappy::MaxCompressedLength(unsigned long)", referenced from:
leveldb::TableBuilder::WriteBlock(leveldb::BlockBuilder*, leveldb::BlockHandle*) in libleveldb.a(table_builder.o)
"snappy::GetUncompressedLength(char const*, unsigned long, unsigned long*)", referenced from:
leveldb::ReadBlock(leveldb::RandomAccessFile*, leveldb::ReadOptions const&, leveldb::BlockHandle const&, leveldb::BlockContents*) in libleveldb.a(format.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
是不是我安装编译有问题?
看来您还需要 linking snappy。我认为这是因为 leveldb 也在使用该库。因此,您需要下载它并将其安装为静态库或共享库,并使用 -lsnappy
link。确保将 linker 标志放在 leveldb 的 linker 标志之后,以便它可以填充 leveldb 缺少的符号。