在编译leveldb-windows前,请确保已编译boost。
一、源码地址
leveldb源码下载地址:https://github.com/google/leveldb/tree/windows
二、Project建立
Step 1: 导入文件
- File -> New -> Project From Existing Code
- 选择创建项目类型,选择Visual C++;点击Next
- 选择leveldb-windows地址,放置于Project file location; 将Project name起名为LevelDB;点击Next
- 选择生成的项目类型:Project type: Static library (LIB) project;点击Next
- 设置调试配置设置:在Preprocessor definitions中填写LEVELDB_PLATFORM_WINDOWS;OS_WIN;点击Next;再点击Finish,完成导入
Step 2: 检查配置
- 配置打成静态库类型:在Configuration Properties -> General -> Configuration Type中修改为Static library (.lib);点击应用
- 配置boost和leveldb-windows:
1)在Configuration Properties -> C/C++ -> General -> Additional Include Directories中加入:
i. boost根目录
ii. leveldb-windows根目录
iii. leveldb-windows/include目录
2)在Configuration Properties -> C/C++ -> Preprocessor中添加:
i. LEVELDB_PLATFORM_WINDOWS
ii. OS_WIN
iii. WIN32
3)在Configuration Properties -> VC++ Directories -> Library Directories中添加:
i. boost/stage/lib
4)选作:遇到了c++标准库里面.h文件找不到的问题,解决方法参考了:https://blog.csdn.net/qq_34362856/article/details/70230337
在Configuration Properties -> VC++ Directories -> Include Directories中添加:
i. C:\Program Files %28x86%29\Windows Kits\10\Include\10.0.17134.0\shared;
ii. C:\Program Files %28x86%29\Windows Kits\10\Include\10.0.17134.0\cppwinrt;
iii. C:\Program Files %28x86%29\Windows Kits\10\Include\10.0.17134.0\ucrt;
iv. C:\Program Files %28x86%29\Windows Kits\10\Include\10.0.17134.0\um;
v. C:\Program Files %28x86%29\Windows Kits\10\Include\10.0.17134.0\winrt
在Configuration Properties -> VC++ Directories -> Library Directories中添加:
i. C:\Program Files %28x86%29\Windows Kits\10\Lib\10.0.17134.0\ucrt\x64;
ii. C:\Program Files %28x86%29\Windows Kits\10\Lib\10.0.17134.0\ucrt_enclave\x64;
iii. C:\Program Files %28x86%29\Windows Kits\10\Lib\10.0.17134.0\um\x64
Step 3: 排除不需要的文件
port_android.cc
port_posix.cc
env_posix.cc
db_bench_sqlite3.cc
db_bench_tree_db.cc
手动删除所有“_test.cc”和“_bench.cc”的文件
Step 4: 必要的源码修改
1)c.cc文件中,注释掉第8行:#include <unistd.h>
2)port.h中,添加:
#elif defined(LEVELDB_PLATFORM_WINDOWS)
#include “port/port_win.h”
3)port_win.h中,注释掉第34行:
#define snprintf _snprintf
三、编译生成64位的lib
如果顺利,编译直接成功。
四、确认LevelDB编译成功,配置并使用
新建一个solution,用测试源码看是否能够跑通。
Step 1: VS2017下的配置:
- 在Configuration Properties -> VC++ Directories -> Library Directories下,添加:
i. D:\Open Libraries\boost_1_65_1\stage\lib;
ii. D:\Open Libraries\leveldb-windows-exp\leveldb-windows\x64\Debug
依赖boost的lib和已经编译好的LevelDB-windows的静态库 - 在Configuration Properties -> C/C++ -> Additional Include Directories中,添加:
i. D:\Open Libraries\boost_1_65_1
ii. D:\Open Libraries\leveldb-windows-exp\leveldb-windows
iii. D:\Open Libraries\leveldb-windows-exp\leveldb-windows\include - 在Configuration Properties -> Linker -> Input -> Additional Dependencies中,添加:
LevelDB.lib (需和打出来的库文件名一致)
Step 2: 测试用源码:
#include <iostream>
#include <leveldb/db.h>
int main()
{
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
std::string dbpath = "testdb";
leveldb::Status status = leveldb::DB::Open(options, dbpath, &db);
std::string key = "test";
std::string val = "test_value";
status = db->Put(leveldb::WriteOptions(), key, val);
std::cout << status.ok() << std::endl;
status = db->Get(leveldb::ReadOptions(), key, &val);
std::cout << status.ok() << std::endl;
std::cout << val << std::endl;
status = db->Delete(leveldb::WriteOptions(), key);
std::cout << status.ok() << std::endl;
system("pause");
return 0;
}
Step 3: 编译并运行
若一切顺利,屏幕显示:
至此,leveldb-windows编译完成。
五、参考
[1] https://blog.csdn.net/ncut_nwpu/article/details/80604356
[2] https://blog.csdn.net/qq_34362856/article/details/70230337