记levelDB在vs2013下编译及测试过程

条件

  1. boost (此次编译采用boost_1-55-0版本)
  2. level windows版本
    获取地址:https://github.com/google/leveldb/tree/windows

过程 (注意boost和levelDB是否都是x86或x64版本)

编译levelDB所需的boost静态库lib

  1. 运行boost根目录下 bootstrap.bat
    ···
  2. 编译所需lib(date_timethreadfilesystem)
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)

  1. 打开VS属性管理器,选择“属性”->“C/C++”->”->“预处理器”,添加预编译宏“WIN32; LEVELDB_PLATFORM_WINDOWS;_CRT_SECURE_NO_WARNINGS;”,

  2. “C/C++”->“常规”->“加包含目录”,添加头文件路径 ..\..\include;..\..;F:\boost_1_55_0;

  3. “库管理器”->“附加库目录”,添加lib所在路径F:\boost_1_55_0\stage\x86\lib;

  4. 修改 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
  1. 在vs项目工程中排除掉无法编译的文件
    如:
  • port/port_android.cc

    • port/port_posix.cc

    • util/env_posix.cc

  1. 生成lib文件

测试

建立测试工程

  1. “C/C++” -> “常规” -> “附加包含目录”,添加levelDB头文件路径E:\mFile\opensrc\leveldb-windows\include;
  2. “链接器” -> “常规” -> “附加库目录”,添加levelDB和boost的lib所在路径E:\mFile\opensrc\leveldb-windows\lib\Win32\;F:\boost_1_55_0\stage\x86\lib;
  3. 测试
#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;
}

猜你喜欢

转载自blog.csdn.net/a731062834/article/details/85230674