1配置clion使用cygwin模拟linux环境,先下载cygwin后配置
2导入源码,配置cmake文件
由于redis
是基于Linux上的Makefile
,所以Windows上需要配置CMakeLists.txt
使用cmake
工具编译运行。github上已经有人尝试编写CMakeLists.txt
文件,项目地址为: https://github.com/LHRchina/redis。本文也是参考该项目的CMakeLists.txt
文件,然后把缺少的库补上成功编译运行的。
redis根目录下配置CMakeLists.txt
,文件内容如下:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
# 项目编译配置来源https://www.cnblogs.com/youyoui/p/16028653.html
# bowen 项目报Dl_info相关错误,在Cygwin下的usr/include/dlfcn.h删除结构体
# 上的#if __GNU_VISIBLE和 #endif注释掉确保结构体Dl_info被编译,改动方式有多种这种修改原始的环境
project(redis VERSION 6.0)
#set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../src)
message(CMAKE_RUNTIME_OUTPUT_DIRECTORY is:${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
#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 ()
message(STATUS "Generating release.h...")
#execute_process(
# COMMAND sh -c ./src/mkreleasehdr.sh
# WORKING_DIRECTORY ${REDIS_ROOT}/src/
#)
message("---begin--")
add_subdirectory(deps)
add_subdirectory(src/modules)
message("---begin")
set(SRC_SERVER_TMPsrc/crcspeed.csrc/crcspeed.hsrc/sha256.csrc/sha256.hsrc/connection.csrc/connection.hsrc/acl.csrc/timeout.csrc/tracking.csrc/tls.csrc/adlist.csrc/ae.csrc/anet.c# windows屏蔽掉下面两个文件,mac系统不需要屏蔽,这两个是mac环境多路复用的库# /usr/local/include/event.h# src/ae_kqueue.csrc/mt19937-64.csrc/mt19937-64.hsrc/monotonic.csrc/monotonic.hsrc/dict.csrc/sds.csrc/zmalloc.csrc/lzf_c.csrc/lzf_d.csrc/pqsort.csrc/zipmap.csrc/sha1.csrc/ziplist.csrc/release.csrc/networking.csrc/util.csrc/object.csrc/db.csrc/replication.csrc/rdb.csrc/t_string.csrc/t_list.csrc/t_set.csrc/t_zset.csrc/evict.csrc/defrag.csrc/module.csrc/quicklist.csrc/expire.csrc/childinfo.csrc/redis-check-aof.csrc/redis-check-rdb.csrc/lazyfree.csrc/geohash.csrc/rax.csrc/geohash_helper.csrc/siphash.csrc/geo.csrc/t_hash.csrc/config.csrc/aof.csrc/pubsub.csrc/multi.csrc/debug.csrc/sort.csrc/intset.csrc/syncio.csrc/cluster.csrc/crc16.csrc/endianconv.csrc/slowlog.csrc/scripting.csrc/bio.csrc/rio.csrc/rand.csrc/memtest.csrc/crc64.csrc/bitops.csrc/sentinel.csrc/notify.csrc/setproctitle.csrc/blocked.csrc/hyperloglog.csrc/latency.csrc/sparkline.csrc/t_stream.csrc/lolwut.csrc/lolwut.hsrc/lolwut5.csrc/lolwut6.csrc/listpack.csrc/localtime.csrc/gopher.c
)
set(SRC_SERVER src/server.c ${SRC_SERVER_TMP})
set(SRC_CLIsrc/cli_common.csrc/mt19937-64.csrc/monotonic.csrc/crcspeed.c# ---src/anet.csrc/sds.csrc/adlist.csrc/redis-cli.csrc/zmalloc.csrc/release.csrc/ae.csrc/crc64.csrc/crc16.csrc/dict.csrc/siphash.c
)
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")# better not to work with jemalloc
endif ()
set(EXECUTABLE_OUTPUT_PATH 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-serverPRIVATE ${REDIS_ROOT}/deps/hiredisPRIVATE ${REDIS_ROOT}/deps/linenoisePRIVATE ${REDIS_ROOT}/deps/lua/src
)target_link_libraries(redis-serverPRIVATE pthreadPRIVATE mPRIVATE luaPRIVATE linenoisePRIVATE hiredis${CMAKE_DL_LIBS}
)target_include_directories(redis-cliPRIVATE ${REDIS_ROOT}/deps/hiredisPRIVATE ${REDIS_ROOT}/deps/linenoisePRIVATE ${REDIS_ROOT}/deps/lua/src
)target_link_libraries(redis-cliPRIVATE pthreadPRIVATE mPRIVATE linenoisePRIVATE hiredis
)
link_directories(deps/hiredis/ deps/linenoise/ diredeps/lua/src)
install(TARGETS redis-serverRUNTIME DESTINATION bin
)
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -c")
src/modules
目录下配置CMakeLists.txt
,文件内容如下:
cmake_minimum_required(VERSION 3.0)
set(CMAKE_BUILD_TYPE "Debug")add_library(helloworld SHARED helloworld.c)
set_target_properties(helloworld PROPERTIES PREFIX "" SUFFIX ".so")add_library(hellotype SHARED hellotype.c)
set_target_properties(hellotype PROPERTIES PREFIX "" SUFFIX ".so")add_library(helloblock SHARED helloblock.c)
set_target_properties(helloblock PROPERTIES PREFIX "" SUFFIX ".so")#add_library(testmodule SHARED testmodule.c)
#set_target_properties(testmodule PROPERTIES PREFIX "" SUFFIX ".so")
deps
目录下配置CMakeLists.txt
,文件内容如下:
add_subdirectory(hiredis) add_subdirectory(linenoise) add_subdirectory(lua)
deps/hiredis
目录下配置CMakeLists.txt
,文件内容如下:
CMAKE_MINIMUM_REQUIRED(VERSION 3.4.0)
INCLUDE(GNUInstallDirs)
PROJECT(hiredis)OPTION(ENABLE_SSL "Build hiredis_ssl for SSL support" OFF)
OPTION(DISABLE_TESTS "If tests should be compiled or not" OFF)
OPTION(ENABLE_SSL_TESTS, "Should we test SSL connections" OFF)MACRO(getVersionBit name)SET(VERSION_REGEX "^#define ${name} (.+)$")FILE(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/hiredis.h"VERSION_BIT REGEX ${VERSION_REGEX})STRING(REGEX REPLACE ${VERSION_REGEX} "\\1" ${name} "${VERSION_BIT}")
ENDMACRO(getVersionBit)getVersionBit(HIREDIS_MAJOR)
getVersionBit(HIREDIS_MINOR)
getVersionBit(HIREDIS_PATCH)
getVersionBit(HIREDIS_SONAME)
SET(VERSION "${HIREDIS_MAJOR}.${HIREDIS_MINOR}.${HIREDIS_PATCH}")
MESSAGE("Detected version: ${VERSION}")PROJECT(hiredis VERSION "${VERSION}")SET(ENABLE_EXAMPLES OFF CACHE BOOL "Enable building hiredis examples")SET(hiredis_sourcesalloc.casync.cdict.chiredis.cnet.cread.csds.csockcompat.c)SET(hiredis_sources ${hiredis_sources})IF(WIN32)ADD_COMPILE_DEFINITIONS(_CRT_SECURE_NO_WARNINGS WIN32_LEAN_AND_MEAN)
ENDIF()ADD_LIBRARY(hiredis SHARED ${hiredis_sources})SET_TARGET_PROPERTIES(hiredisPROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUEVERSION "${HIREDIS_SONAME}")
IF(WIN32 OR MINGW)TARGET_LINK_LIBRARIES(hiredis PRIVATE ws2_32)
ENDIF()TARGET_INCLUDE_DIRECTORIES(hiredis PUBLIC $<INSTALL_INTERFACE:.> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)CONFIGURE_FILE(hiredis.pc.in hiredis.pc @ONLY)INSTALL(TARGETS hiredisEXPORT hiredis-targetsRUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})INSTALL(FILES hiredis.h read.h sds.h async.h alloc.hDESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis)INSTALL(DIRECTORY adaptersDESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis)INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis.pcDESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)export(EXPORT hiredis-targetsFILE "${CMAKE_CURRENT_BINARY_DIR}/hiredis-targets.cmake"NAMESPACE hiredis::)SET(CMAKE_CONF_INSTALL_DIR share/hiredis)
SET(INCLUDE_INSTALL_DIR include)
include(CMakePackageConfigHelpers)
configure_package_config_file(hiredis-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/hiredis-config.cmakeINSTALL_DESTINATION ${CMAKE_CONF_INSTALL_DIR}PATH_VARS INCLUDE_INSTALL_DIR)INSTALL(EXPORT hiredis-targetsFILE hiredis-targets.cmakeNAMESPACE hiredis::DESTINATION ${CMAKE_CONF_INSTALL_DIR})INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis-config.cmakeDESTINATION ${CMAKE_CONF_INSTALL_DIR})IF(ENABLE_SSL)IF (NOT OPENSSL_ROOT_DIR)IF (APPLE)SET(OPENSSL_ROOT_DIR "/usr/local/opt/openssl")ENDIF()ENDIF()FIND_PACKAGE(OpenSSL REQUIRED)SET(hiredis_ssl_sourcesssl.c)ADD_LIBRARY(hiredis_ssl SHARED${hiredis_ssl_sources})IF (APPLE)SET_PROPERTY(TARGET hiredis_ssl PROPERTY LINK_FLAGS "-Wl,-undefined -Wl,dynamic_lookup")ENDIF()SET_TARGET_PROPERTIES(hiredis_sslPROPERTIESWINDOWS_EXPORT_ALL_SYMBOLS TRUEVERSION "${HIREDIS_SONAME}")TARGET_INCLUDE_DIRECTORIES(hiredis_ssl PRIVATE "${OPENSSL_INCLUDE_DIR}")TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE ${OPENSSL_LIBRARIES})IF (WIN32 OR MINGW)TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE hiredis)ENDIF()CONFIGURE_FILE(hiredis_ssl.pc.in hiredis_ssl.pc @ONLY)INSTALL(TARGETS hiredis_sslEXPORT hiredis_ssl-targetsRUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})INSTALL(FILES hiredis_ssl.hDESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis)INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl.pcDESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)export(EXPORT hiredis_ssl-targetsFILE "${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-targets.cmake"NAMESPACE hiredis::)SET(CMAKE_CONF_INSTALL_DIR share/hiredis_ssl)configure_package_config_file(hiredis_ssl-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-config.cmakeINSTALL_DESTINATION ${CMAKE_CONF_INSTALL_DIR}PATH_VARS INCLUDE_INSTALL_DIR)INSTALL(EXPORT hiredis_ssl-targetsFILE hiredis_ssl-targets.cmakeNAMESPACE hiredis::DESTINATION ${CMAKE_CONF_INSTALL_DIR})INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-config.cmakeDESTINATION ${CMAKE_CONF_INSTALL_DIR})
ENDIF()IF(NOT DISABLE_TESTS)ENABLE_TESTING()ADD_EXECUTABLE(hiredis-test test.c)IF(ENABLE_SSL_TESTS)ADD_DEFINITIONS(-DHIREDIS_TEST_SSL=1)TARGET_LINK_LIBRARIES(hiredis-test hiredis hiredis_ssl)ELSE()TARGET_LINK_LIBRARIES(hiredis-test hiredis)ENDIF()ADD_TEST(NAME hiredis-testCOMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test.sh)
ENDIF()# Add examples
IF(ENABLE_EXAMPLES)ADD_SUBDIRECTORY(examples)
ENDIF(ENABLE_EXAMPLES)
deps/linenoise
目录下配置CMakeLists.txt
,文件内容如下:
add_library(linenoise linenoise.c)
deps/lua
目录下配置CMakeLists.txt
,文件内容如下:
set(LUA_SRCsrc/lauxlib.csrc/liolib.csrc/lopcodes.csrc/lstate.csrc/lobject.csrc/print.csrc/lmathlib.csrc/loadlib.csrc/lvm.csrc/lfunc.csrc/lstrlib.csrc/lua.csrc/linit.csrc/lstring.csrc/lundump.csrc/luac.csrc/ltable.csrc/ldump.csrc/loslib.csrc/lgc.csrc/lzio.csrc/ldblib.csrc/strbuf.csrc/lmem.csrc/lcode.csrc/ltablib.csrc/lua_struct.csrc/lapi.csrc/lbaselib.csrc/lua_cmsgpack.csrc/ldebug.csrc/lparser.csrc/lua_cjson.csrc/fpconv.csrc/lua_bit.csrc/llex.csrc/ltm.csrc/ldo.c)
add_library(lua STATIC ${LUA_SRC})
配置完之后,右键点击根目录下的CMakeLists.txt
,选择Reload CMake Project
。此时右上角会出现redis-server|Debug
的运行选项,点击调试可以运行redis-server
。
问题:
之前我编译成功了,但不久之后选择在linux环境编译这个项目,导致缓存一直存在报错问题。
如果当前重载项目没有可运行选项,在工具栏里选择清空缓存,重载cmake项目,不行的话退出Clion然后清除cmake缓存,删除编译目标文件再重载cmake。之前我编译成功后来不知道动了哪里的环境一直编译报错,后来发现是在此期间我更新了Clion导致的,clion做了优化之前运行redis-server服务时不会弹出cmd框,这导致在clion关闭运行后后面的服务还在运行现在是只要关闭cmd弹框就停止运行,效果更好。
报错问题汇总
如果使用的是其他版本的redis,则需要根据编译运行信息调整上面的CMakeLists.txt
文件,相应的调整配置,一般是缺失编译文件或者多余代码。
编译报错error: unknown type name 'Dl_info'
首次编译的时候会遇到编译错误:
FAILED: CMakeFiles/redis-server.dir/src/debug.c.o
/cygdrive/g/Projects/Git_Projects/redis-6.2.6/src/debug.c: In function 'dumpX86Calls':
/cygdrive/g/Projects/Git_Projects/redis-6.2.6/src/debug.c:1753:5: error: unknown type name 'Dl_info'
没有类型D1_info
,因为这是一个调试文件,然后其中的函数没有调用,我的解决办法是把相关的函数注释掉。主要是src/debug.c
文件中的dumpX86Calls
函数和dumpCodeAroundEIP
两个函数注释掉就可以了。
启动报错Address already in use
启动的时候报错端口被占用,redis-server默认使用6379端口,要去报该端口没有被占用才行,获取可以添加运行参数或者配置文件使用其他端口。
注意 编译源码最开始时有特殊的环境条件报错这个需要修改cygwin环境的内容,由于第一次编译很长时间忘记具体的内容,大概是环境检测上的当编译报错时可以网上收到。