写程序大体步骤为:
1.用编辑器编写源代码,如.c文件。
2.用编译器编译代码生成目标文件,如.o。
3.用链接器连接目标代码生成可执行文件,如.exe。
但如果源文件太多,一个一个编译时就会特别麻烦,于是人们想到,为什么不设计一种类似批处理的程序,来批处理编译源文件呢,于是就有了make工具,它是一个自动化编译工具,你可以使用一条命令实现完全编译。但是你需要编写一个规则文件,make依据它来批处理编译,这个文件就是makefile,所以编写makefile文件也是一个程序员所必备的技能。
对于一个大工程,编写makefile实在是件复杂的事,于是人们又想,为什么不设计一个工具,读入所有源文件之后,自动生成makefile呢,于是就出现了cmake工具,它能够输出各种各样的makefile或者project文件,从而帮助程序员减轻负担。但是随之而来也就是编写cmakelist文件,它是cmake所依据的规则。所以在编程的世界里没有捷径可走,还是要脚踏实地的。
所以流程如下:
一个程序,在linux下运行,你要写一份makefile;如果要弄到其他平台,这个makefile就用不了了,要再写一份。
所以,为了跨平台,出现了cmake,cmake是让程序员用统一的语法来写cmake文件,然后cmake会帮助我们生成对应的平台下的makefile。
由于学习过程中参考的书籍为《Redis 设计与实现》,因此源码版本也跟本书保持一致。
步骤
- 下载源码
git clone git@github.com:huangz1990/redis-3.0-annotated.git
- deps/hiredis目录下新增CMakeLists.txt
add_library(hiredis STATIC
hiredis.c
net.c
dict.c
net.c
sds.c
async.c
)
- deps/linenoise目录下新增CMakeLists.txt
add_library(linenoise
linenoise.c
)
- deps/lua目录下新增CMakeLists.txt
set(LUA_SRC
src/lauxlib.c
src/liolib.c
src/lopcodes.c
src/lstate.c
src/lobject.c
src/print.c
src/lmathlib.c
src/loadlib.c
src/lvm.c
src/lfunc.c
src/lstrlib.c
src/lua.c
src/linit.c
src/lstring.c
src/lundump.c
src/luac.c
src/ltable.c
src/ldump.c
src/loslib.c
src/lgc.c
src/lzio.c
src/ldblib.c
src/strbuf.c
src/lmem.c
src/lcode.c
src/ltablib.c
src/lua_struct.c
src/lapi.c
src/lbaselib.c
src/lua_cmsgpack.c
src/ldebug.c
src/lparser.c
src/lua_cjson.c
src/llex.c
src/ltm.c
src/ldo.c
)
add_library(lua STATIC ${LUA_SRC})
- deps目录下新增CMakeLists.txt
add_subdirectory(hiredis)
add_subdirectory(linenoise)
add_subdirectory(lua)
- redis-3.0-annotated目录下新增CMakeLists.txt5
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(redis-3.0-annotated-cmake-in-clion 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()
message(STATUS "Generating release.h...")
execute_process(
COMMAND sh -c ./mkreleasehdr.sh
WORKING_DIRECTORY ${REDIS_ROOT}/src/
)
add_subdirectory(deps)
set(SRC_SERVER
src/adlist.c src/ae.c src/anet.c src/dict.c src/redis.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/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
)
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
)
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
# better not to work with jemalloc
endif()
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/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 pthread
PRIVATE m
PRIVATE lua
PRIVATE linenoise
PRIVATE hiredis
)
target_link_libraries(redis-cli
PRIVATE pthread
PRIVATE m
PRIVATE linenoise
PRIVATE hiredis
)
link_directories(deps/hiredis/ deps/linenoise/ diredeps/lua/src)
导入CLion
将项目导入CLion,导入时选择不覆盖已有的CMakeLists.txt。运行
在CLion中选择redis-server,选择运行/调试,即可成功运行。
源码地址
参考https://github.com/htw0056/redis-3.0-annotated-cmake-in-clion,clone该项目导入CLion即可直接运行。