在這裡使用centos 6
若尚未安裝cmake套件,請參考以下方法
# wget https://cmake.org/files/v3.11/cmake-3.11.1.tar.gz
# tar xvf cmake-3.11.1.tar.gz
# cd cmake-3.11.1
# ./bootstrap
編譯安裝
# gmake
以上注意,gcc需要版本4.8以上,才有支援C++11,不然會報錯
更新gcc可以參考以下網址(必須先有舊版本gcc)
https://edwards.sdsu.edu/research/c11-on-centos-6/
下載原始碼
# git clone https://github.com/google/leveldb.git
# cd leveldb
# mkdir -p build
# cd build
# cmake ..
# make
程式試跑
編譯完成後,我們建立一個自己的資料夾來儲存自己的程式,來使用LevelDB
寫一個程式名為 app_test.cc
寫一個程式名為 app_test.cc
# cd leveldb
# mkdir -p app
-
- #include "leveldb/db.h"
- #include <cassert>
- #include <iostream>
-
- using namespace std;
- using namespace leveldb;
-
- int main() {
- leveldb::DB *db;
- leveldb::Options options;
- options.create_if_missing = true;
- leveldb::Status status = leveldb::DB::Open(options, "testdb", &db);
- assert(status.ok());
- status = db->Put(WriteOptions(), "P76061433", "QK");
- assert(status.ok());
- string res;
- status = db->Get(ReadOptions(), "P76061433", &res);
- assert(status.ok());
- cout << res << endl;
-
- delete db;
- return 0;
- }
接下來修改leveldb/CMakeLists.txt文件
leveldb_test("${PROJECT_SOURCE_DIR}/db/c_test.c")
leveldb_test("${PROJECT_SOURCE_DIR}/db/fault_injection_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/issues/issue178_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/issues/issue200_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/util/env_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/util/status_test.cc")
if(NOT BUILD_SHARED_LIBS)
+ leveldb_test("${PROJECT_SOURCE_DIR}/app/app_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/autocompact_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/corruption_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/db_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/dbformat_test.cc")
leveldb_test("${PROJECT_SOURCE_DIR}/db/filename_test.cc")
修改這行就完成了,因為CMakeLists.txt發生變動,所以需要重新改動
# cd leveldb/build
# cmake ..
# make
如此就可以執行app_test了,執行結果如下
# ./app_test
QK
可以看到value輸出為QK,代表成功囉!
沒有留言:
張貼留言