条件
- 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;
}