- 可以在各种平台上提供二进制和源代码发行版。
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.h"
DESTINATION include
)
## 测试
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.236")
do_test(Tutorial 7 "7 is 2.645")
do_test(Tutorial 25 "25 is 5")
do_test(Tutorial -25 "-25 is (-nan|nan|0)")
do_test(Tutorial 0.0001 "0.0001 is 0.01")
## Packaging an Installer
# 此模块将打包项目当前平台所需的任何运行时库
include(InstallRequiredSystemLibraries)
# 设置一些 CPack 变量来存储这个项目的许可证和版本信息
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set(CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
# 为源包选择一种文件格式
# ZIP (.zip)
# TGZ (.tar.gz)
# TBZ2 (.tar.bz2)
# 等格式
set(CPACK_SOURCE_GENERATOR "TGZ")
include(CPack)
二、像往常一样 build 工程
cmake --build . --config Release
三、执行打包命令
cpack
:构建二进制发行版cpack -G ZIP -C Debug
:指定发行包的文件格式和配置类型cpack --config CPackSourceConfig.cmake
:打包带有源码的可发行包