LevelDB是一个由谷歌开源的高性能Key-Value存储库。笔者最早是在.net平台下使用,直接安装的nuget包。最近准备在C++编译,并阅读理解其中的代码。
Leveldb Github仓库介绍.png

编译步骤

  1. 获取源代码 https://github.com/google/leveldb
    git clone --recurse-submodules https://github.com/google/leveldb.git
    加上--recurse-submodules 的选项来拉取leveldb的开发依赖,其实就是google的测试和benchmark框架。leveldb本身可以不依赖其他第三方库
  2. 按照官方指引,通过CMake生成Visual Studio Solution解决方案

    mkdir build
    cd build
    cmake -G "Visual Studio 17" -A x64 ..

    如果希望编译32位版本,可以去掉-A x64 的选项

  3. 在Visual Studio中编译
    双击打开build目录下的sln文件进行编译。
    或者按照官方教程使用命令行编译(需要VS的环境变量)
    devenv /build Debug leveldb.sln
  1. 自行拷贝链接库和头文件到需要的项目,或者配置CMAKE_INSTALL_PREFIX进行自动安装

Visual Studio测试,其中Compression相关的测试失败了,其余测试均通过
Visual Studio 测试

踩坑实录

今天踩了两个CMake的坑:

  • CMake你告诉我忽略.. 路径?

CMake错误截图
一开始也是盲目自信,报了这个warning之后不知所措,没有看到画红线的这句话。其实是因为新建了build 目录后没有切换过去找不到文件。

  • x64架构的cmake配置问题
    按照官方教程,一顿操作猛如虎,瞬间变成智障250。说好的加Win64 呢?
    cmake -G "Visual Studio 17 Win64" ..
    结果来了这么一堆输出
CMake Error: Could not create named generator Visual Studio 17 Win64

Generators
* Visual Studio 17 2022        = Generates Visual Studio 2022 project files.
                                 Use -A option to specify architecture.
  Visual Studio 16 2019        = Generates Visual Studio 2019 project files.
                                 Use -A option to specify architecture.
  Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
                                 Optional [arch] can be "Win64" or "ARM".
  Visual Studio 11 2012 [arch] = Deprecated.  Generates Visual Studio 2012
                                 project files.  Optional [arch] can be
                                 "Win64" or "ARM".
  Visual Studio 9 2008 [arch]  = Generates Visual Studio 2008 project files.
                                 Optional [arch] can be "Win64" or "IA64".
  Borland Makefiles            = Generates Borland makefiles.
  NMake Makefiles              = Generates NMake makefiles.
  NMake Makefiles JOM          = Generates JOM makefiles.
  MSYS Makefiles               = Generates MSYS makefiles.
  MinGW Makefiles              = Generates a make file for use with
                                 mingw32-make.
  Green Hills MULTI            = Generates Green Hills MULTI files
                                 (experimental, work-in-progress).
  Unix Makefiles               = Generates standard UNIX makefiles.
  Ninja                        = Generates build.ninja files.
  Ninja Multi-Config           = Generates build-<Config>.ninja files.
  Watcom WMake                 = Generates Watcom WMake makefiles.
  CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.
  CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.
  CodeBlocks - NMake Makefiles JOM
                               = Generates CodeBlocks project files.
  CodeBlocks - Ninja           = Generates CodeBlocks project files.
  CodeBlocks - Unix Makefiles  = Generates CodeBlocks project files.
  CodeLite - MinGW Makefiles   = Generates CodeLite project files.
  CodeLite - NMake Makefiles   = Generates CodeLite project files.
  CodeLite - Ninja             = Generates CodeLite project files.
  CodeLite - Unix Makefiles    = Generates CodeLite project files.
  Eclipse CDT4 - NMake Makefiles
                               = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - MinGW Makefiles
                               = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Ninja         = Generates Eclipse CDT 4.0 project files.
  Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
  Kate - MinGW Makefiles       = Generates Kate project files.
  Kate - NMake Makefiles       = Generates Kate project files.
  Kate - Ninja                 = Generates Kate project files.
  Kate - Unix Makefiles        = Generates Kate project files.
  Sublime Text 2 - MinGW Makefiles
                               = Generates Sublime Text 2 project files.
  Sublime Text 2 - NMake Makefiles
                               = Generates Sublime Text 2 project files.
  Sublime Text 2 - Ninja       = Generates Sublime Text 2 project files.
  Sublime Text 2 - Unix Makefiles
                               = Generates Sublime Text 2 project files.

仔细看了一下输出,自从VS2019之后用-A x64 来指定架构,也解决了。