尝试在 Windows 上使用hiredis,构建一个普通的c++ 应用程序来读取和写入redis。问题是hiredis 不正式支持在Windows 上构建。如何使用hiredis和c ++构建应用程序?
问问题
4141 次
2 回答
7
经过大量搜索和尝试后,我提出了使用 CMake 的这种方法,尽管这些信息也可以用于与其他构建系统一起构建。
抓取微软开源redis项目并打开项目文件msvs\RedisServer.sln
以所需的配置(调试、发布……)构建项目
创建一个名为hiredis_ROOT
(控制面板->系统->高级系统设置->环境变量)的环境变量,并将其值设置为解压的redis文件夹
使用下面的内容创建一个 cmake 查找模块,并将名称放在cmake
文件夹下的项目根目录下Findhiredis.cmake
# set search hint directories
set(
hiredis_POSSIBLE_ROOT_PATHS
$ENV{hiredis_ROOT}
/usr/local
/usr
)
# find hiredis include directory
# =================================================================================
find_path(
hiredis_INCLUDE_DIR
NAME hiredis/hiredis.h
HINTS ${hiredis_POSSIBLE_ROOT_PATHS}
PATH_SUFFIXES "include" "deps"
)
if(NOT hiredis_INCLUDE_DIR)
message(STATUS "Checking for hiredis... no")
message(STATUS "Could not find include path for hiredis, try setting hiredis_ROOT")
return()
endif()
# find hiredis library
# =================================================================================
# library for debug builds
find_library(
hiredis_LIBRARY_DEBUG
NAMES hiredis
HINTS ${hiredis_POSSIBLE_ROOT_PATHS}
PATH_SUFFIXES "msvs/x64/Debug"
DOC "hiredis library for debug builds"
)
# library for release builds
find_library(
hiredis_LIBRARY_RELEASE
NAMES hiredis
HINTS ${hiredis_POSSIBLE_ROOT_PATHS}
PATH_SUFFIXES "msvs/x64/Release"
DOC "hiredis library for release builds"
)
# create library name for linking
set(hiredis_LIBRARY "")
if(hiredis_LIBRARY_DEBUG AND hiredis_LIBRARY_RELEASE)
set(hiredis_LIBRARY "optimized;${hiredis_LIBRARY_RELEASE};debug;${hiredis_LIBRARY_DEBUG}")
elseif(hiredis_LIBRARY_DEBUG)
set(hiredis_LIBRARY "${hiredis_LIBRARY_DEBUG}")
elseif(hiredis_LIBRARY_RELEASE)
set(hiredis_LIBRARY "${hiredis_LIBRARY_RELEASE}")
endif()
# check the result
if(NOT hiredis_LIBRARY)
message(STATUS "Checking for hiredis... no")
message(STATUS "hiredis include directory: ${hiredis_INCLUDE_DIR}")
message(STATUS "Could not find hiredis library")
return()
endif()
# find hiredis' interop library
# =================================================================================
find_library(
hiredis_interop_LIBRARY_DEBUG
NAMES Win32_Interop
HINTS ${hiredis_POSSIBLE_ROOT_PATHS}
PATH_SUFFIXES "msvs/x64/Debug"
DOC "Windows modified interop library for debug builds"
)
find_library(
hiredis_interop_LIBRARY_RELEASE
NAMES Win32_Interop
HINTS ${hiredis_POSSIBLE_ROOT_PATHS}
PATH_SUFFIXES "msvs/x64/Release"
DOC "Windows modified interop library for release builds"
)
set(hiredis_interop_LIBRARY "")
if(hiredis_interop_LIBRARY_DEBUG AND hiredis_interop_LIBRARY_RELEASE)
set(hiredis_interop_LIBRARY "optimized;${hiredis_interop_LIBRARY_RELEASE};debug;${hiredis_interop_LIBRARY_DEBUG}")
elseif(hiredis_interop_LIBRARY_DEBUG)
set(hiredis_interop_LIBRARY "${hiredis_interop_LIBRARY_DEBUG}")
elseif(hiredis_interop_LIBRARY_RELEASE)
set(hiredis_interop_LIBRARY "${hiredis_interop_LIBRARY_RELEASE}")
endif()
# check the result
if(NOT hiredis_interop_LIBRARY)
message(STATUS "Checking for hiredis' interop... no")
message(STATUS "hiredis include directory: ${hiredis_INCLUDE_DIR}")
message(STATUS "Could not find hiredis interop library")
return()
endif()
# Sum up libraries
# =================================================================================
set(Ws2_32_LIBRARY "optimized;Ws2_32;debug;Ws2_32")
set(
hiredis_LIBRARIES
${hiredis_LIBRARY}
${hiredis_interop_LIBRARY}
${Ws2_32_LIBRARY}
)
# everything is found. just finish up
# =================================================================================
set(hiredis_FOUND TRUE CACHE BOOL "Whether hiredis is found on the system or not")
set(hiredis_INCLUDE_DIR ${hiredis_INCLUDE_DIR} CACHE PATH "hiredis include directory")
set(hiredis_LIBRARIES ${hiredis_LIBRARIES} CACHE FILEPATH "hiredis library for linking against")
message(STATUS "Checking for hiredis... yes")
CMakeLists.txt
使用以下命令在顶层包含 find 模块:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
find_package(hiredis REQUIRED)
在您的 cmake 项目中设置自定义定义、包含路径和链接库:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_CRT_SECURE_NO_WARNINGS")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
include_directories(${hiredis_INCLUDE_DIR})
target_link_libraries(my-redis-application ${hiredis_LIBRARIES})
而已。你完成了。
于 2015-08-24T19:52:41.557 回答
0
我可以提供一个替代的 cpp 库bredis:它不使用官方的 redis 解析器(hiredis
库),这就是它在 Windows(c++ 14)上完美编译的原因。
于 2019-04-14T16:54:45.010 回答