美文网首页
LevelDB在windows上编译

LevelDB在windows上编译

作者: ssochi | 来源:发表于2018-12-26 10:48 被阅读0次

下载Boost

https://www.boost.org/users/download/

编译Boost

  1. 打开windows左下角菜单按钮
  2. 搜索本机工具命令提示符
  3. 选择x64版本打开
  4. 在控制台内直接输入你的boost根地址(不加cd)
  5. 键入 bootstrap.bat ,运行该批处理
  6. bootstrap.bat生成了一些文件,其中包括b2.exe
    键入 b2.exe runtime-link=static,这里生成的是(sgd的lib,如果不加则生成gd的lib,具体的区别可以参考https://stackoverflow.com/questions/6014517/whats-the-difference-between-mt-gd-and-mt-s-library),生成lib的过程会持续很长时间,玩局游戏吧!

配置LevelDB环境

这里使用VS2017进行操作

  1. 配置属性 -> C/C++ -> 常规 -> 附加包含目录中添加 boost的根目录
  2. 配置属性 -> 链接器 -> 常规 -> 附加库目录中添加 boost根目录下的stage\lib目录
  3. clone leveldb 项目
    git clone https://github.com/google/leveldb.git
    git checkout windows
  4. 用VS引入LevelDB项目,选择应用程序
  5. 在配置属性 -> C/C++ -> 常规 -> 附加包含目录添加
    leveldb根目录,leveldb/include,boost根目录
  6. 在配置属性 -> C/C++ -> 预处理器-> 预处理器定义中添加LEVELDB_PLATFORM_WINDOWS; OS_WIN两个宏,并将_WINDOWS改成_CONSOLE
  7. 从项目中删除
    port/port_android.cc
    port/port_posix.cc
    util/env_posix.cc
    doc/db_bench_tree_db.cc
    doc/db_bench_sqlite3.cc
    db/c_test.c
    *_test.cc
    *_bench.cc
  8. 修改port/port.h文件
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#ifndef STORAGE_LEVELDB_PORT_PORT_H_
#define STORAGE_LEVELDB_PORT_PORT_H_

#include <string.h>

// Include the appropriate platform specific file below.  If you are
// porting to a new platform, see "port_example.h" for documentation
// of what the new port_<platform>.h file must provide.
#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)
#include "port/port_win.h"
#endif

#endif  // STORAGE_LEVELDB_PORT_PORT_H_

9.添加unistd.h

#pragma once
/** This file is part of the Mingw32 package.
* unistd.h maps (roughly) to io.h
*/

#ifndef _UNISTD_H
#define _UNISTD_H
#include <io.h>
#include <process.h>
#endif /* _UNISTD_H */

10.将port_win.h中的(如下代码)注掉

#define snprintf _snprintf

11.编写一个测试用例

#include "leveldb/db.h"

#include "db/db_impl.h"
#include "db/filename.h"
#include "db/version_set.h"
#include "db/write_batch_internal.h"
#include "leveldb/cache.h"
#include "leveldb/env.h"
#include "leveldb/table.h"
#include "port/port.h"

#include "util/hash.h"
#include "util/logging.h"
#include "util/mutexlock.h"
#include "util/testharness.h"
#include "util/testutil.h"
#include <cassert>
#include <iostream>
int main() {

    leveldb::DB* db;
    leveldb::Options options;
    options.create_if_missing = true;
    leveldb::Status status = leveldb::DB::Open(options, "lev", &db);
    //std::cout << "connect status : " << status.ok() << std::endl;

    db->Put(leveldb::WriteOptions(), "say", "hello leveldb");

    std::string val = "";
    db->Get(leveldb::ReadOptions(), "say", &val);

    std::cout << val << std::endl;

}

12.运行

>leveldb.exe
hello leveldb

相关文章

网友评论

      本文标题:LevelDB在windows上编译

      本文链接:https://www.haomeiwen.com/subject/jzwclqtx.html