1. 程式人生 > 其它 >leveldb——leveldb入門篇之Linux下編譯配置和使用

leveldb——leveldb入門篇之Linux下編譯配置和使用

1.首先,從github上下載leveldb原始碼的zip檔案
使用命令

#wget https://codeload.github.com/google/leveldb/zip/master
1
2.下載完後,使用#file master檢視,發現它是一個.zip檔案,所以要使用#unzip master來解壓縮,解壓縮後會在當前檔案下生成一個leveldb-master的資料夾
使用命令

#cd leveldb-master && make all
1
就會自動編譯安裝。

3.等待編譯安裝完成後,在leveldb-master資料夾下使用命令

cp out-shared/libleveldb.so* /usr/local/lib && cp -R include/* /usr/local/include
1
4.寫第一個測試程式

#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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
編譯程式:

g++ main.cpp -lpthread -lleveldb -o main
1
這個時候會出現如下錯誤:
./leveldb: error while loading shared libraries: libleveldb.so.1: cannot open shared object file: No such file or directory

解決辦法:
在/etc/ld.so.conf.d的資料夾中 新建一個檔案,命名為level.conf,用vim開啟它,加入/usr/local/lib這一行,儲存之後,再執行:#/sbin/ldconfig –v更新一下配置即可。

————————————————
版權宣告:本文為CSDN博主「konsy_dong」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/sinat_36053757/article/details/70597877