迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 数据库 >

如何在mac系统下使用clion调试redis源码

作者:迹忆客 时间:2021/05/06 浏览次数:

clion主要使用的是cmake + make 进行编译。所以对于redis4来说,主要的就是先编写 CMakeLists.txt 文件。

CmakeLists.txt 文件

redis4/CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(redis4)

set(CMAKE_BUILD_TYPE "Debug")

get_filename_component(REDIS_ROOT "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE)

add_subdirectory(deps)
add_subdirectory(src/modules)

set(SRC_SERVER_TMP
        src/adlist.c
        src/ae.c
        src/anet.c
        src/dict.c
        src/sds.c
        src/zmalloc.c
        src/lzf_c.c
        src/lzf_d.c
        src/pqsort.c
        src/zipmap.c
        src/sha1.c
        src/ziplist.c
        src/release.c
        src/networking.c
        src/util.c
        src/object.c
        src/db.c
        src/replication.c
        src/rdb.c
        src/t_string.c
        src/t_list.c
        src/t_set.c
        src/t_zset.c
        src/evict.c
        src/defrag.c
        src/module.c
        src/quicklist.c
        src/expire.c
        src/childinfo.c
        src/redis-check-aof.c
        src/redis-check-rdb.c
        src/lazyfree.c
        src/geohash.c
        src/rax.c
        src/geohash_helper.c
        src/siphash.c
        src/geo.c
        src/t_hash.c
        src/config.c
        src/aof.c
        src/pubsub.c
        src/multi.c
        src/debug.c
        src/sort.c
        src/intset.c
        src/syncio.c
        src/cluster.c
        src/crc16.c
        src/endianconv.c
        src/slowlog.c
        src/scripting.c
        src/bio.c
        src/rio.c
        src/rand.c
        src/memtest.c
        src/crc64.c
        src/bitops.c
        src/sentinel.c
        src/notify.c
        src/setproctitle.c
        src/blocked.c
        src/hyperloglog.c
        src/latency.c
        src/sparkline.c
        )
set(SRC_SERVER src/server.c ${SRC_SERVER_TMP})

set(SRC_CLI
        src/anet.c
        src/sds.c
        src/adlist.c
        src/redis-cli.c
        src/zmalloc.c
        src/release.c
        src/anet.c
        src/ae.c
        src/crc64.c
        )
set(EXECUTABLE_OUTPUT_PATH src)

link_directories(deps/linenoise/ deps/lua/src deps/hiredis)
add_executable(redis-server ${SRC_SERVER})
target_include_directories(redis-server
        PRIVATE ${REDIS_ROOT}/deps/linenoise
        PRIVATE ${REDIS_ROOT}/deps/hiredis
        PRIVATE ${REDIS_ROOT}/deps/lua/src)
target_link_libraries(redis-server
        PRIVATE pthread
        PRIVATE m
        PRIVATE lua
        PRIVATE linenoise
        PRIVATE hiredis)

add_executable(redis-cli ${SRC_CLI})
target_include_directories(redis-cli
        PRIVATE ${REDIS_ROOT}/deps/linenoise
        PRIVATE ${REDIS_ROOT}/deps/hiredis
        PRIVATE ${REDIS_ROOT}/deps/lua/src)

target_link_libraries(redis-cli
        PRIVATE pthread
        PRIVATE m
        PRIVATE linenoise
        PRIVATE hiredis)

redis4/src/modules/CMakeLists.txt

cmake_minimum_required(VERSION 3.9)
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")

redis4/deps/CMakeLists.txt

add_subdirectory(linenoise)
add_subdirectory(lua)
add_subdirectory(hiredis)

redis4/deps/lua/CMakeLists.txt

add_subdirectory(src)

redis4/deps/lua/src/CMakeLists.txt

set(LUA_SRC
        lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c
        lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c
        lundump.c lvm.c lzio.c strbuf.c fpconv.c
        lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c ltablib.c
        lstrlib.c loadlib.c linit.c lua_cjson.c lua_struct.c lua_cmsgpack.c
        lua_bit.c
        )

add_library(lua STATIC
        ${LUA_SRC}
        )

redis4/deps/linenoise/CMakeLists.txt

add_library(linenoise
        linenoise.c
        )

redis4/deps/hiredis/CMakeLists.txt

add_library(hiredis STATIC
        hiredis.c
        net.c
        dict.c
        net.c
        sds.c
        async.c
        read.c
        )

所有的CMakeLists.txt文件都已经编写完成。接下来在CLion中新建redis4项目

CLion 新建 redis4 项目

选择 File->New Cmake Project from Source... 然后选择我们的redis4文件夹。

clion导入redis源码项目

注意: 不要覆盖上面编写的CMakeLists.txt,所以可以选择 Opening Existing Project 而不要选择 Import as a new Cmake Project

新建项目之后,CLion会自动使用cmake构建项目。构建完成之后,会发现有下面这些运行项。

clion自动使用cmak工具自动构建项目

选择redis-server 然后选择右边的 run/debug 按钮

出现如下结果说明成功。

clion中Redis服务开启成功

这样我们就可以打断点进行调试了。上面的各项配置如果顺利的话是可以直接跑通的。然而现实总是残酷的,经常会出现这样那样的问题,这就要根据实际问题实际解决。 比如说当前我们是在mac系统下,如果换到linux系统进行远程调试,那是不是还那么顺利呢?这些就需要我们实际碰到的时候再去根据情况去解决了。

除非注明转载,本站文章均为原创,欢迎转载,转载请以链接形式注明出处

本文地址:

迹忆客

专注技术分享,项目实战分享!

技术宅 乐于分享 7年编程经验
社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

热门文章

热门标签