参考自:https://www.jianshu.com/p/ae7b61c89e23 我是2021.7.2日clone的unstable分支,对参考文章做出了一些调整
deps\hdr_histogram\CMakeLists.txt add_library(hdr_histogram hdr_histogram.c)
deps\hiredis\CMakeLists.txt 当前版本已有生成动态库的操作(ADD_LIBRARY(hiredis SHARED ${hiredis_sources}))所以无需重复添加。 添加测试用例
SET(ENABLE_EXAMPLES OFF CACHE BOOL "Enable building hiredis examples")
修改为
SET(ENABLE_EXAMPLES ON )
之后在deps\hiredis\examples\CMakeLists.txt最下方,按照官方提供的例子追加即可
deps\linenoise\CMakeLists.txt add_library(linenoise linenoise.c)
deps\lua\CMakeLists.txtaux_source_directory(./src LUA_SRC)
#去除lua的main
LIST(REMOVE_ITEM LUA_SRC "./src/luac.c" "./src/lua.c")
add_library(lua STATIC ${LUA_SRC})
#生成调试程序不能用lua因为上边已经将它声明为库名
add_executable(lua.exe "./src/lua.c" ${LUA_SRC})
deps\CMakeLists.txt add_subdirectory(hiredis)
add_subdirectory(linenoise)
add_subdirectory(lua)
add_subdirectory(hdr_histogram)
项目根文件 CMakeLists.txt 注意这个文件我增添了一个标识 set(GENERATING_RELEASE OFF)用于是否生成release.h文件,如果第一次编译就将OFF改为ON之后改为OFF就行不然每次都要执行生成文件的那个脚本 cmake_minimum_required(VERSION 3.16)
project(redis VERSION 3.0)
if (NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type defined; defaulting to 'Debug'")
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING
"The type of build. Possible values are: Debug, Release, RelWithDebInfo and MinSizeRel.")
endif()
message(STATUS "Host is: ${CMAKE_HOST_SYSTEM}. Build target is: ${CMAKE_SYSTEM}")
get_filename_component(REDIS_ROOT "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE)
message(STATUS "Project root directory is: ${REDIS_ROOT}")
# Just for debugging when handling a new platform.
if (false)
message("C++ compiler supports these language features:")
foreach(i ${CMAKE_CXX_COMPILE_FEATURES})
message(" ${i}")
endforeach()
endif()
set(GENERATING_RELEASE OFF)
IF(GENERATING_RELEASE)
message("Generating release.h")
execute_process(
COMMAND sh -c ./mkreleasehdr.sh
WORKING_DIRECTORY ${REDIS_ROOT}/src/
)
ENDIF()
add_subdirectory(deps)
set(SRC_SERVER
adlist.c quicklist.c ae.c anet.c dict.c server.c sds.c zmalloc.c lzf_c.c lzf_d.c pqsort.c zipmap.c sha1.c
ziplist.c release.c networking.c util.c object.c db.c replication.c rdb.c t_string.c t_list.c t_set.c t_zset.c
t_hash.c config.c aof.c pubsub.c multi.c debug.c sort.c intset.c syncio.c cluster.c crc16.c endianconv.c slowlog.c
scripting.c bio.c rio.c rand.c memtest.c crcspeed.c crc64.c bitops.c sentinel.c notify.c setproctitle.c blocked.c
hyperloglog.c latency.c sparkline.c redis-check-rdb.c redis-check-aof.c geo.c lazyfree.c module.c evict.c expire.c
geohash.c geohash_helper.c childinfo.c defrag.c siphash.c rax.c t_stream.c listpack.c localtime.c lolwut.c lolwut5.c
lolwut6.c acl.c tracking.c connection.c tls.c sha256.c timeout.c setcpuaffinity.c monotonic.c mt19937-64.c
)
set(SRC_CLI
anet.c adlist.c dict.c redis-cli.c zmalloc.c release.c ae.c redisassert.c crcspeed.c crc64.c siphash.c crc16.c monotonic.c cli_common.c mt19937-64.c
)
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
# better not to work with jemalloc
endif()
#源文件添加路径前缀
list(TRANSFORM SRC_SERVER PREPEND "./src/" )
list(TRANSFORM SRC_CLI PREPEND "./src/" )
add_executable(redis-server ${SRC_SERVER})
add_executable(redis-cli ${SRC_CLI})
set_property(TARGET redis-server PROPERTY C_STANDARD 99)
set_property(TARGET redis-server PROPERTY CXX_STANDARD 11)
set_property(TARGET redis-server PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET redis-cli PROPERTY C_STANDARD 99)
set_property(TARGET redis-cli PROPERTY CXX_STANDARD 11)
set_property(TARGET redis-cli PROPERTY CXX_STANDARD_REQUIRED ON)
target_include_directories(redis-server
PRIVATE ${REDIS_ROOT}/deps/hiredis
PRIVATE ${REDIS_ROOT}/deps/linenoise
# PRIVATE ${REDIS_ROOT}/deps/jemalloc
# PRIVATE ${REDIS_ROOT}/deps/hdr_histogram
PRIVATE ${REDIS_ROOT}/deps/lua/src
)
target_include_directories(redis-cli
PRIVATE ${REDIS_ROOT}/deps/hiredis
PRIVATE ${REDIS_ROOT}/deps/linenoise
# PRIVATE ${REDIS_ROOT}/deps/jemalloc
PRIVATE ${REDIS_ROOT}/deps/lua/src
)
target_link_libraries(redis-server
PRIVATE lua
PRIVATE linenoise
PRIVATE hiredis
PRIVATE hdr_histogram
)
target_link_libraries(redis-cli
PRIVATE linenoise
PRIVATE hiredis
)
注意 cmake-build-debug\deps\hiredis\cyghiredis.dll文件移动到cmake-build-debug\cyghiredis.dll 不然会出现错误