記levelDB在vs2013下編譯及測試過程
阿新 • • 發佈:2019-01-06
條件
- boost (此次編譯採用boost_1-55-0版本)
- level windows版本
獲取地址:https://github.com/google/leveldb/tree/windows
過程 (注意boost和levelDB是否都是x86或x64版本)
編譯levelDB所需的boost靜態庫lib
- 執行boost根目錄下
bootstrap.bat
··· - 編譯所需lib(
date_time
、thread
、filesystem
)
b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="F:\boost_1_55_0\stage\x86" link=static runtime-link=static threading=multi debug release
b2 stage --toolset=msvc-12.0 --with-thread --stagedir="F:\boost_1_55_0\stage\x86" link=static runtime-link=static threading=multi debug release
b2 stage --toolset=msvc-12.0 --with-filesystem --stagedir="F:\boost_1_55_0\stage\x86" link=static runtime-link=static threading=multi debug release
編譯 levelDB(基於vs2013)
-
開啟VS屬性管理器,選擇“屬性”->“C/C++”->”->“前處理器”,新增預編譯巨集“
WIN32; LEVELDB_PLATFORM_WINDOWS;_CRT_SECURE_NO_WARNINGS;
”, -
“C/C++”->“常規”->“加包含目錄”,新增標頭檔案路徑
..\..\include;..\..;F:\boost_1_55_0;
-
“庫管理器”->“附加庫目錄”,新增lib所在路徑
F:\boost_1_55_0\stage\x86\lib;
-
修改
port.h
檔案,加上 Windows平臺的標頭檔案port/port_win.h
#if defined(LEVELDB_PLATFORM_POSIX)
# include "port/port_posix.h"
#elif defined(LEVELDB_PLATFORM_CHROMIUM)
# include "port/port_chromium.h"
#elif defined(LEVELDB_PLATFORM_ANDROID)
# include "port/port_android.h"
#elif defined(LEVELDB_PLATFORM_WINDOWS) //新新增的windows版本編譯選項
# include "port/port_win.h" //新新增的windows版本編譯選項
#endif
- 在vs專案工程中排除掉無法編譯的檔案
如:
-
port/port_android.cc
-
port/port_posix.cc
-
util/env_posix.cc
等
-
- 生成lib檔案
測試
建立測試工程
- “C/C++” -> “常規” -> “附加包含目錄”,新增levelDB標頭檔案路徑
E:\mFile\opensrc\leveldb-windows\include;
- “連結器” -> “常規” -> “附加庫目錄”,新增levelDB和boost的lib所在路徑
E:\mFile\opensrc\leveldb-windows\lib\Win32\;F:\boost_1_55_0\stage\x86\lib;
- 測試
#include <string.h>
#include <iostream>
#include "leveldb/db.h"
#ifdef _DEBUG
#pragma comment(lib, "LevelDBd.lib")
#else
#pragma comment(lib, "LevelDB.lib")
#endif // _DEBUG
int main()
{
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
if (!status.ok())
{
}
std::string key = "key";
std::string value = "value";
status = db->Put(leveldb::WriteOptions(), key, value);
if (!status.ok())
{
}
status = db->Get(leveldb::ReadOptions(), key, &value);
if (!status.ok())
{
}
std::cout << value << std::endl;
std::string key2 = "key2";
status = db->Put(leveldb::WriteOptions(), key2, value);
if (!status.ok())
{
}
status = db->Delete(leveldb::WriteOptions(), key);
if (!status.ok())
{
}
status = db->Get(leveldb::ReadOptions(), key2, &value);
if (!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;
getchar();
return 0;
}