zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Windows11环境编译leveldb

2023-02-25 18:19:00 时间

亲爱的读者朋友们,躺平区UP腆着脸来更新了,这半年时间经历了几次内心的仰卧起坐,最终还是彻底的躺了,这次挣扎着再次坐了起来,希望能给大家带来一点不一样的东西。好了,废话不多说了,Let's go !!!

环境准备

  1. 下载安装CMake
  2. 下载安装Visual Studio

clone 代码仓库

git clone --recurse-submodules https://github.com/google/leveldb.git

编译

生成VS工程

cd leveldb

cmake CMakeLists.txt

D:\study\leveldb [main ≡]> cmake CMakeLists.txt
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.22621.
-- The C compiler identification is MSVC 19.31.31107.0
-- The CXX compiler identification is MSVC 19.31.31107.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for unistd.h
-- Looking for unistd.h - not found
-- Looking for crc32c_value in crc32c
-- Looking for crc32c_value in crc32c - not found
-- Looking for snappy_compress in snappy
-- Looking for snappy_compress in snappy - not found
-- Looking for malloc in tcmalloc
-- Looking for malloc in tcmalloc - not found
-- Looking for fdatasync
-- Looking for fdatasync - not found
-- Looking for F_FULLFSYNC
-- Looking for F_FULLFSYNC - not found
-- Looking for O_CLOEXEC
-- Looking for O_CLOEXEC - not found
-- Performing Test HAVE_CLANG_THREAD_SAFETY
-- Performing Test HAVE_CLANG_THREAD_SAFETY - Failed
-- Performing Test LEVELDB_HAVE_NO_MISSING_FIELD_INITIALIZERS
-- Performing Test LEVELDB_HAVE_NO_MISSING_FIELD_INITIALIZERS - Failed
-- Performing Test HAVE_CXX17_HAS_INCLUDE
-- Performing Test HAVE_CXX17_HAS_INCLUDE - Success
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
-- Found Threads: TRUE
-- Found Python: D:/envs/anaconda3/python.exe (found version "3.9.12") found components: Interpreter
-- Found Git: C:/Program Files/Git/cmd/git.exe (found version "2.38.1.windows.1")
-- git Version: v1.5.3-7d0d9061
-- Version: 1.5.3
-- Performing Test HAVE_CXX_FLAG_EHS_
-- Performing Test HAVE_CXX_FLAG_EHS_ - Success
-- Performing Test HAVE_CXX_FLAG_EHA_
-- Performing Test HAVE_CXX_FLAG_EHA_ - Success
-- Performing Test HAVE_STD_REGEX
-- Performing Test HAVE_STD_REGEX
-- Performing Test HAVE_STD_REGEX -- success
-- Performing Test HAVE_GNU_POSIX_REGEX
-- Performing Test HAVE_GNU_POSIX_REGEX
-- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile
-- Performing Test HAVE_POSIX_REGEX
-- Performing Test HAVE_POSIX_REGEX
-- Performing Test HAVE_POSIX_REGEX -- failed to compile
CMake Warning at third_party/benchmark/CMakeLists.txt:282 (message):
  Using std::regex with exceptions disabled is not fully supported


-- Performing Test HAVE_STEADY_CLOCK
-- Performing Test HAVE_STEADY_CLOCK
-- Performing Test HAVE_STEADY_CLOCK -- success
-- Looking for sqlite3_open in sqlite3
-- Looking for sqlite3_open in sqlite3 - not found
-- Performing Test HAVE_KYOTOCABINET
-- Performing Test HAVE_KYOTOCABINET - Failed
-- Configuring done
-- Generating done
-- Build files have been written to: D:/study/leveldb

打开 leveldb.sln 生成解决方案

至此 leveldb 编译完成,接下来创建 demo 工程。

DEMO

创建解决方案 leveldb_test 将 leveldb 代码目录下 includeDebug/leveldb.lib 拷贝到 leveldb_test/third_party/leveldb

添加依赖 属性 -> c/c++ -> 常规 -> 附加包含目录

连接器 -> 输入 -> 附加依赖项

leveldb_test.cpp

创建 leveldb_test.cpp demo文件

#include "leveldb/db.h"
#include<iostream>

int main() {
 // 声明
 leveldb::DB* mydb;
 leveldb::Options myoptions;
 leveldb::Status mystatus;

 // 创建
 myoptions.create_if_missing = true;
 mystatus = leveldb::DB::Open(myoptions, "testdb", &mydb);

 // 写入数据
 std::string key = "jacky";
 std::string value = "maimai & doudou";
 if (mystatus.ok()) {
  mydb->Put(leveldb::WriteOptions(), key, value);
 }

 // 读取数据
 std::string key_ = "jacky";
 std::string val_ = "";
 mydb->Get(leveldb::ReadOptions(), key_, &val_);

 std::cout << key_ << ": " << val_ << std::endl;
}

执行结果

- END -

你好,我是 +7 ,一个大数据领域的硬核原创作者。 做过后端架构、数据库中间件、数据平台&架构、产品。 专注大数据领域,数据库领域实时动态&技术提升&个人成长&职场进阶,欢迎关注。