CMake 打包可安装程序

可以在各种平台上提供二进制和源代码发行版。 InstallRequiredSystemLibraries CPack 一、需要在顶层 CMakeLists.txt 文件的底部添加几行代码 # 设置CMake版本最低要求 cmake_minimum_required(VERSION 3.10)# 设置项目名称和版本 project(Tutorial VERSION 3.1)# 指定 C++ 标准 set(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_STANDARD_REQUIRED True)# 设置编译选项 option(USE_MYMATH "Use tutorial provided math implementation" ON)# 生成一个头文件,传递 CMake 的一些设置到源代码 configure_file(TutorialConfig.h.in TutorialConfig.h)# 添加 MathFunctions library # add_subdirectory(MathFunctions) if(USE_MYMATH) add_subdirectory(MathFunctions) list(APPEND EXTRA_LIBS MathFunctions)endif()# 添加源码文件和生成的目标文件的名称 add_executable(Tutorial main.cpp)# target_link_libraries(Tutorial PUBLIC MathFunctions) target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})# 添加头文件查找路径 # target_include_directories(Tutorial PUBLIC # "${PROJECT_BINARY_DIR}" # "${PROJECT_SOURCE_DIR}/MathFunctions" # ) target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" )## 安装 install(TARGETS Tutorial DESTINATION bin)install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig....

March 1, 2022 · 1 min · Rick Cui

CMake 添加自定义命令和生成文件

add_custom_command MathFunctions 工程的 CMakeLists.txt # 添加可执行程序 MakeTable add_executable(MakeTable MakeTable.cxx)# 添加一个自定义命令,指定如何通过运行 MakeTable 生成 Table.h add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h DEPENDS MakeTable )# 将生成的 Table.h 添加到库 MathFunctions 的源代码列表 add_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h )# 将当前二进制目录添加到包含目录列表中,这样 Table.h 才能被 mysqrt.cxx 找到并包含 target_include_directories(MathFunctions INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_BINARY_DIR} )install(TARGETS MathFunctions DESTINATION lib)install(FILES MathFunctions.h DESTINATION include)install(FILES ${CMAKE_CURRENT_BINARY_DIR}/Table.h DESTINATION include)生成的 Table.h 文件 double sqrtTable[] = { 0, 1, 1.41421, 1.73205, 2, 2.23607, 2.44949, 2.64575, 2.82843, 3, 0}; Tutorial....

February 28, 2022 · 1 min · Rick Cui

CMake 添加系统自检

CheckSymbolExists: 考虑在项目中添加一些代码,这些代码依赖于目标平台可能没有的特性 target_compile_definitions add_library(MathFunctions mysqrt.cxx)target_include_directories(MathFunctions INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})# does this system provide the log and exp functions? include(CheckSymbolExists)check_symbol_exists(log "math.h" HAVE_LOG)check_symbol_exists(exp "math.h" HAVE_EXP)# 如果平台中没有找到 log 和 exp,我们需要链接到 m 库 if(NOT (HAVE_LOG AND HAVE_EXP)) unset(HAVE_LOG CACHE) unset(HAVE_EXP CACHE) # 设置 m 库并再次尝试 set(CMAKE_REQUIRED_LIBRARIES "m") check_symbol_exists(log "math.h" HAVE_LOG) check_symbol_exists(exp "math.h" HAVE_EXP) if(HAVE_LOG AND HAVE_EXP) target_link_libraries(MathFunctions PRIVATE m) endif()endif()# 如果可用,使用 target_compile_definitions() 指定 HAVE LOG 和 HAVE EXP 作为私有编译定义 # 在项目配置 预处理器定义中添加 HAVE LOG 和 HAVE EXP 两个宏定义 if(HAVE_LOG AND HAVE_EXP) target_compile_definitions(MathFunctions PRIVATE "HAVE_LOG" "HAVE_EXP")endif()# install install(TARGETS MathFunctions DESTINATION lib)install(FILES MathFunctions....

February 27, 2022 · 1 min · Rick Cui

CMake 安装和测试

Install Rules 在库的 CMakeLists.txt 文件末尾添加 install add_library(MathFunctions mysqrt.cxx)target_include_directories(MathFunctions INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})install(TARGETS MathFunctions DESTINATION lib)install(FILES MathFunctions.h DESTINATION include)在顶层 CMakeLists.txt 的末尾添加 ... ... ## 安装 install(TARGETS Tutorial DESTINATION bin)install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h" DESTINATION include )Testing Support 在顶层 CMakeLists.txt 的末尾添加 ... ... ## 测试 enable_testing()# does the application run add_test(NAME Runs COMMAND Tutorial 25)# does the usage message work? add_test(NAME Usage COMMAND Tutorial)# 设置校验信息 set_tests_properties(Usage PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number" )# define a function to simplify adding tests function(do_test target arg result) add_test(NAME Comp${arg} COMMAND ${target} ${arg}) set_tests_properties(Comp${arg} PROPERTIES PASS_REGULAR_EXPRESSION ${result} )endfunction()# do a bunch of result based tests do_test(Tutorial 4 "4 is 2")do_test(Tutorial 9 "9 is 3")do_test(Tutorial 5 "5 is 2....

February 27, 2022 · 1 min · Rick Cui

CMake 添加依赖库和使用条件

添加依赖库和使用条件 CMakeLists.txt 注意编译选项、生成配置文件和条件编译三部分的顺序 # 设置CMake版本最低要求 cmake_minimum_required(VERSION 3.10)# 设置项目名称和版本 project(Tutorial VERSION 3.1)# 指定 C++ 标准 set(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_STANDARD_REQUIRED True)# 设置编译选项 option(USE_MYMATH "Use tutorial provided math implementation" ON)# 生成一个头文件,传递 CMake 的一些设置到源代码 configure_file(TutorialConfig.h.in TutorialConfig.h)# 添加 MathFunctions library # add_subdirectory(MathFunctions) if(USE_MYMATH) add_subdirectory(MathFunctions) list(APPEND EXTRA_LIBS MathFunctions) list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")endif()# 添加源码文件和生成的目标文件的名称 add_executable(Tutorial main.cpp)# target_link_libraries(Tutorial PUBLIC MathFunctions) target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})# 添加头文件查找路径 # target_include_directories(Tutorial PUBLIC # "${PROJECT_BINARY_DIR}" # "${PROJECT_SOURCE_DIR}/MathFunctions" # ) target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" ${EXTRA_INCLUDES} )子模块(库)的 CMakeLists.txt 文件 add_library(MathFunctions mysqrt....

February 27, 2022 · 1 min · Rick Cui