安装:
进入git clone下来的项目中 make 此时leveldb/下多出out-shared和out-static目录,其中out-shared/下有: db db_bench helpers libleveldb.so libleveldb.so.1 libleveldb.so.1.19 port table util等文件 然后执行: sudo cp out-shared/libleveldb.so* /usr/local/lib & sudo cp -R include/* /usr/local/include 将动态链接库.so文件拷贝到系统环境中。
测试代码:
#include <cassert>
#include <iostream>
#include <string>
#include <cstdlib>
#include <leveldb/db.h>
int main()
{
leveldb::DB *db;
leveldb::Options options;
leveldb::Status status;
std::
string key1 =
"key1";
std::
string val1 =
"val1", val;
options.create_if_missing =
true;
status = leveldb::DB::Open(options,
"/tmp/testdb", &db);
if (!status.ok())
{
std::
cout << status.ToString() <<
std::endl;
exit(
1);
}
status = db->Put(leveldb::WriteOptions(), key1, val1);
if (!status.ok())
{
std::
cout << status.ToString() <<
std::endl;
exit(
2);
}
status = db->Get(leveldb::ReadOptions(), key1, &val);
if (!status.ok())
{
std::
cout << status.ToString() <<
std::endl;
exit(
3);
}
std::
cout <<
"Get val: " << val <<
std::endl;
status = db->Delete(leveldb::WriteOptions(), key1);
if (!status.ok())
{
std::
cout << status.ToString() <<
std::endl;
exit(
4);
}
status = db->Get(leveldb::ReadOptions(), key1, &val);
if (!status.ok())
{
std::
cout << status.ToString() <<
std::endl;
exit(
5);
}
std::
cout <<
"Get val: " << val <<
std::endl;
return 0;
}
编译:
g
++ -o testdb testleveldb
.cpp
-lleveldb
运行时会出现 :./testdb: error while loading shared libraries: libleveldb.so.1: cannot open shared object file: No such file or directory 这是由于lib库(libleveldb.so.1)虽然添加到了系统,但是系统配置尚未更新,所以需要更新下就可以:
sudo /sbin/ldconfig -v
再运行,结果如下:
转载请注明原文地址: https://ju.6miu.com/read-22051.html