目录
cmake
cmake demo
根目录
- ${PROJECT_SOURCE_DIR}根目录下CMakelists.txt
# 指定CMake编译最低要求版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 判断系统处理器类型
IF(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_amd64")
message(STATUS "x86_64 architecture detected")
ELSEIF(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "AMD64")
message(STATUS "x64 architecture detected")
ELSE()
message(STATUS "x32 x64 or x86 isn't detected")
ENDIF()
# 默认构建类型设置为Release 用法:cmake .. -DCMAKE_BUILD_TYPE=Release
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
endif ()
# # 编译选项设置
SET(CMAKE_C_FLAGS_RELEASE "-D_REETRANT -DNDEBUG -fPIC -Wall -Wextra -pedantic -O3 -std=gnu99 -v")
SET(CMAKE_C_FLAGS_DEBUG "-D_REETRANT -g -fPIC -Wall -Wextra -pedantic -O0 -v")
SET(CMAKE_CXX_FLAGS_RELEASE "-Wall -O2")
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
# 给项目命名
PROJECT(khl_project)
# 指定头文件目录
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
# 编译条件设置,让程序按照cmake设置编译条件编译
# case 0
OPTION(mps_en "Enable the mps: on/off" on)
IF(mps_en STREQUAL "off")
MESSAGE(STATUS "Config: mps=off")
ADD_DEFINITIONS("-Dmps_en=0")
ELSE()
MESSAGE(STATUS "Config: mps=on")
ADD_DEFINITIONS("-Dmps_en=1")
ENDIF()
# case 1
OPTION(daemon_en "Enable the daemon: on/off" on)
IF(daemon_en STREQUAL "off")
MESSAGE(STATUS "Config: daemon=off")
ADD_DEFINITIONS("-Ddaemon_en=0")
ELSE()
MESSAGE(STATUS "Config: daemon=on")
ADD_DEFINITIONS("-Ddaemon_en=1")
ENDIF()
# case 2
OPTION(loglevel "set the log level(0-fatal, 1-error, 2-warn, 3-info, 4-debug, 5-trace, default 3)" 3)
IF(loglevel)
MESSAGE(STATUS "Config: loglevel=${loglevel}")
ADD_DEFINITIONS("-Dloglevel=${loglevel}")
ENDIF()
# 设置可执行二进制文件输出位置
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin/)
# 设置子目录并构建,追加变量LINK_LIB变量为simple_log PUBLIC动态库log,指定头文件地址暴露
ADD_SUBDIRECTORY(log)
SET(LINK_LIB ${LINK_LIB} log)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/log/simple_log)
# 追加LINK_LIB变量为gcc_s,设置子目录并构建,追加LINK_LIB变量为libuv PUBLIC静态库uv_a,指定头文件地址进行暴露
SET(LINK_LIB ${LINK_LIB} gcc_s)
ADD_SUBDIRECTORY(3rd)
SET(LINK_LIB ${LINK_LIB} uv_a)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/3rd/libuv/include)
# 设置子目录并构建
ADD_SUBDIRECTORY(lib)
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(test)
ADD_SUBDIRECTORY(tools)
3rd
- ${PROJECT_SOURCE_DIR}/3rd目录下
# 指定CMake编译最低要求版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 设置子目录并构建
ADD_SUBDIRECTORY(libuv)
- ${PROJECT_SOURCE_DIR}/log目录下
# 指定CMake编译最低要求版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 设置子目录并构建
ADD_SUBDIRECTORY(simple_log)
- ${PROJECT_SOURCE_DIR}/log/simple_log目录下
# 指定CMake编译最低要求版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 指定头文件
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
# 设置LIBLOG_SRC变量
SET(LIBLOG_SRC
log.h
log.c
)
# 取变量LIBLOG_SRC值,指定生成动态库liblog.so
ADD_LIBRARY(log SHARED
${LIBLOG_SRC}
)
# 为动态库liblog.so指定头文件目录为当前目录CMAKE_CURRENT_SOURCE_DIR
target_include_directories(log PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
# 设置log动态库版本VERSION=${${PROJECT_NAME}_VERSION}
# 设置api版本SOVERSION=${${PROJECT_NAME}_SOVERSION
SET_TARGET_PROPERTIES(log PROPERTIES VERSION ${${PROJECT_NAME}_VERSION} SOVERSION ${${PROJECT_NAME}_SOVERSION})
# 将头文件安装到CMAKE_INSTALL_INCLUDEDIR目录下
# 将库安装到CMAKE_INSTALL_LIBDIR目录下
install(FILES log.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS log LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
src
- ${PROJECT_SOURCE_DIR}/src目录下
# 指定CMake编译最低要求版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 设置子目录并构建
ADD_SUBDIRECTORY(client)
ADD_SUBDIRECTORY(codetest)
ADD_SUBDIRECTORY(kernel)
ADD_SUBDIRECTORY(server)
ADD_SUBDIRECTORY(pti)
ADD_SUBDIRECTORY(daemon)
- ${PROJECT_SOURCE_DIR}/src/client目录下
# FLEX扫描器和BISON解析器,用于构建编译器
FIND_PACKAGE(FLEX)
FIND_PACKAGE(BISON)
# 引入线程包
FIND_PACKAGE(Threads)
add_executable(Network main.cpp)
target_link_libraries (${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT}) //链接 Thread 库
# target_link_libraries (${PROJECT_NAME} pthread)
- ${PROJECT_SOURCE_DIR}/src/codetest目录下
- ${PROJECT_SOURCE_DIR}/src/kernel目录下
- ${PROJECT_SOURCE_DIR}/src/server目录下
# 指定CMake编译最低要求版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 设置变量TARGET=server
SET(TARGET server)
# 指定头文件路径在当前目录下的/hh /mm /ll /
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/hh)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/mm)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/ll)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/)
# 查询当前目录下的/ /hh /mm /ll 包含的所有.c文件
FILE(GLOB SERVER_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/*.c"
"${CMAKE_CURRENT_SOURCE_DIR}/hh/*.c"
"${CMAKE_CURRENT_SOURCE_DIR}/mm/*.c"
"${CMAKE_CURRENT_SOURCE_DIR}/ll/*.c")
# 指定头文件路径为项目根目录/log/simple_log
# 追加SERVER_SRCS变量内容log.c
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/log/simple_log)
SET(SERVER_SRCS ${SERVER_SRCS} ${PROJECT_SOURCE_DIR}/log/simple_log/log.c)
# 判断系统处理器类型
IF(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_amd64")
message(STATUS "x86_64 architecture detected")
ELSEIF(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "AMD64")
message(STATUS "x64 architecture detected")
ELSE()
message(STATUS "x32 x64 or x86 isn't detected")
ENDIF()
# 生成可执行文件
ADD_EXECUTABLE(${TARGET} ${SERVER_SRCS})
# 链接到库
TARGET_LINK_LIBRARIES(${TARGET} ${LINK_LIB})
# 安装目标文件TARGET为可执行二进制文件(RUNTIME)到/bin路径下
install (TARGETS ${TARGET}
RUNTIME
DESTINATION ${CMAKE_INSTALL_BINDIR})
- ${PROJECT_SOURCE_DIR}/src/pti目录下
- ${PROJECT_SOURCE_DIR}/src/daemon目录下
test
- ${PROJECT_SOURCE_DIR}/test目录下
# 指定CMake编译最低要求版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 寻找线程包环境
FIND_PACKAGE(Threads REQUIRED)
# 使能cmake外部项目
include(ExternalProject)
# 下载安装googletest release版本
ExternalProject_Add(
gtest
# URL https://github.com/google/googletest/archive/release-1.12.1.zip
URL ${CMAKE_CURRENT_SOURCE_DIR}/gtest/release-1.12.1.zip
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/gtest
# Disable install step
INSTALL_COMMAND ""
TIMEOUT 60
GIT_PROGRESS true
)
# 设置子目录并构建
ADD_SUBDIRECTORY(common_test)
# ADD_SUBDIRECTORY(googletest)
# 从cmake外部项目中获取源文件地址和二进制文件地址
ExternalProject_Get_Property(gtest SOURCE_DIR BINARY_DIR)
# 构建libgtest静态库 IMPORTED外部引用库转为内部使用 GLOBAL全局可见
# 设置库路径及选项
add_library(libgtest IMPORTED STATIC GLOBAL)
add_dependencies(libgtest gtest)
set_target_properties(libgtest PROPERTIES
"IMPORTED_LOCATION" "${binary_dir}/lib/libgtest.a"
"IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
)
# 构建libgmock静态库 IMPORTED外部引用库转为内部使用 GLOBAL全局可见
# 设置库路径及选项
add_library(libgmock IMPORTED STATIC GLOBAL)
add_dependencies(libgmock gtest)
set_target_properties(libgmock PROPERTIES
"IMPORTED_LOCATION" "${binary_dir}/lib/libgmock.a"
"IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
)
# 指定暴露gtest头文件
include_directories("${SOURCE_DIR}/googletest/include"
"${SOURCE_DIR}/googlemock/include")
- ${PROJECT_SOURCE_DIR}/test/common_test目录下
# 指定CMake编译最低要求版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 查找当前目录路径下文件及其他路径下的文件
FILE(GLOB TEST_SRCS
# "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp"
# "${CMAKE_CURRENT_SOURCE_DIR}/test_mpu_function.cpp"
# "${CMAKE_CURRENT_SOURCE_DIR}/test_mpu_function_class.cpp"
# "${PROJECT_SOURCE_DIR}/tools/pcie_tool/ll_ioctl.c"
)
# 生成可执行文件
ADD_EXECUTABLE(test ${TEST_SRCS})
# 目标文件test链接到库libgtest libgmock
TARGET_LINK_LIBRARIES(test
libgtest
libgmock
)
# 添加测试
add_test(NAME test
COMMAND test)
cmake语法知识
- 目录
CMAKE_SOURCE_DIR
为项目顶级根目录CMAKE_BINARY_DIR
为项目构建输出的build目录CMAKE_CURRENT_SOURCE_DIR
为CMakeLists.txt当前目录CMAKE_CURRENT_BINARY_DIR
为当前CMakeLists.txt构建输出的目录CMAKE_CURRENT_LIST_DIR
为当前执行CMakeLists.txt文件所在目录CMAKE_CURRENT_LIST_FILE
为当前执行CMakeLists.txt文件所在路径CMAKE_CURRENT_LIST_LINE
为当前执行CMakeLists.txt文件所在行号
- 版本号
CMAKE_VERSION
为cmake版本CMAKE_MAJOR_VERSION
为cmake主要版本CMAKE_MINOR_VERSION
为cmake次要版本CMAKE_PATCH_VERSION
为cmake补丁版本CMAKE_TWEAK_VERSION
为cmake微调版本
- STREQUAL cmake专用关键字,用于比较字符串,字符串相同时返回ture,eg:
IF(mps_en STREQUAL "off")
- 编译选项统一用
cmake .. -Dmps_en=on -DBUILD_TESTING=on
设置- 编译选项无依赖OPTION
OPTION(mps_en "Enable the mps: on/off" on)
- 编译选项依赖cmake_dependent_option,当"BUILD_TESTING;CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR"中的条件全都为真时(即BUILD_TESTING=ON,CMAKE_SOURCE_DIR=PROJECT_SOURCE_DIR),LIBUV_BUILD_TESTS=ON;否则LIBUV_BUILD_TESTS=OFF
cmake_dependent_option(LIBUV_BUILD_TESTS "Build the unit tests when BUILD_TESTING is enabled and we are the root project" ON "BUILD_TESTING;CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR" OFF) cmake_dependent_option(LIBUV_BUILD_BENCH "Build the benchmarks when building unit tests and we are the root project" ON "LIBUV_BUILD_TESTS" OFF)
list(APPEND uv_cflags ${lint-utf8-msvc} )
向列表uv_cflags追加${lint-utf8-msvc}元素,其中APPEND为关键字