该文档是基于CMake的官方教程翻译而来,并稍微添加了自己的理解:
cmake的官方网站为:CMake Tutorial
The CMake tutorial provides a step-by-step guide that covers common build system issues that CMake helps address. Seeing how various topics all work together in an example project can be very helpful.
CMake 教程提供了一个循序渐进的指南,涵盖了 CMake 可帮助解决的常见构建系统问题。在一个示例项目中了解各个主题是如何协同工作的,会非常有帮助。
The tutorial source code examples are available in this archive. Each step has its own subdirectory containing code that may be used as a starting point. The tutorial examples are progressive so that each step provides the complete solution for the previous step.
本文档中提供了教程源代码示例。每个步骤都有自己的子目录,其中包含可用作起点的代码。教程示例是循序渐进的,因此每一步都提供了前一步的完整解决方案。
Next suppose that we want to distribute our project to other people so that they can use it. We want to provide both binary and source distributions on a variety of platforms. This is a little different from the install we did previously in Installing and Testing, where we were installing the binaries that we had built from the source code. In this example we will be building installation packages that support binary installations and package management features. To accomplish this we will use CPack to create platform specific installers. Specifically we need to add a few lines to the bottom of our top-level CMakeLists.txt file.
接下来,假设我们想把项目发布给其他人,让他们也能使用。我们希望在各种平台上提供二进制版本和源代码版本。这与我们之前在 "安装与测试 "中进行的安装有些不同,在 "安装与测试 "中,我们安装的是根据源代码构建的二进制文件。在本例中,我们将构建支持二进制安装和软件包管理功能的安装包。为此,我们将使用 CPack 创建特定平台的安装程序。具体来说,我们需要在顶层 CMakeLists.txt 文件底部添加几行。
CMakeLists.txt
include(InstallRequiredSystemLibraries)
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}")
set(CPACK_SOURCE_GENERATOR "TGZ")
include(CPack)
That is all there is to it. We start by including InstallRequiredSystemLibraries. This module will include any runtime libraries that are needed by the project for the current platform. Next we set some CPack variables to where we have stored the license and version information for this project. The version information was set earlier in this tutorial and the License.txt has been included in the top-level source directory for this step. The CPACK_SOURCE_GENERATOR variable selects a file format for the source package.
仅此而已。我们首先要安装 InstallRequiredSystemLibraries
。该模块将包含项目在当前平台下所需的运行库。接下来,我们设置一些 CPack
变量,以存储项目的许可证和版本信息。版本信息已在本教程前面设置,License.txt
已包含在本步骤的顶级源代码目录中。CPACK_SOURCE_GENERATOR
变量用于选择源代码包的文件格式。
Finally we include the CPack module which will use these variables and some other properties of the current system to setup an installer.
最后,我们加入 CPack
模块,该模块将使用这些变量和当前系统的一些其他属性来设置安装程序。
The next step is to build the project in the usual manner and then run the cpack executable. To build a binary distribution, from the binary directory run:
下一步是按常规方式构建项目,然后运行 cpack 可执行文件。要构建二进制发行版,请在二进制目录下运行
cpack
To specify the generator, use the -G option. For multi-config builds, use -C to specify the configuration. For example:
要指定生成器,请使用 -G 选项。对于多配置编译,使用 -C 来指定配置。例如
cpack -G ZIP -C Debug
For a list of available generators, see cpack-generators(7) or call cpack --help. An archive generator like ZIP creates a compressed archive of all installed files.
有关可用生成器的列表,请参阅 cpack-generators(7),或调用 cpack --help 。archive generator(如 ZIP)会创建一个包含所有已安装文件的压缩包。
To create an archive of the full source tree you would type:
要创建完整源代码树的存档,您可以键入
cpack --config CPackSourceConfig.cmake
Alternatively, run make package or right click the Package target and Build Project from an IDE.
或者,运行 make package 或右键单击软件包目标,然后从集成开发环境中构建项目。
Run the installer found in the binary directory. Then run the installed executable and verify that it works.
运行二进制目录中的安装程序。然后运行已安装的可执行文件并验证其是否正常运行。
cmake_minimum_required(VERSION 3.15)
# set the project name and version
project(Tutorial VERSION 1.0)
# specify the C++ standard
add_library(tutorial_compiler_flags INTERFACE)
target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
# add compiler warning flags just when building this project via
# the BUILD_INTERFACE genex
set(gcc_like_cxx "$" )
set(msvc_cxx "$" )
target_compile_options(tutorial_compiler_flags INTERFACE
"$<${gcc_like_cxx}:$>"
"$<${msvc_cxx}:$>"
)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
# add the MathFunctions library
add_subdirectory(MathFunctions)
# add the executable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
# add the install targets
install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION include
)
# enable testing
include(CTest)
# 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")
include(InstallRequiredSystemLibraries)
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}")
set(CPACK_SOURCE_GENERATOR "TGZ")
include(CPack)
test@test:~/sda3/work/cmake$ mkdir Step9_build
test@test:~/sda3/work/cmake$ cd Step9_build/
test@test:~/sda3/work/cmake/Step9_build$ ls
test@test:~/sda3/work/cmake/Step9_build$ cmake ../Step9
-- The C compiler identification is GNU 10.5.0
-- The CXX compiler identification is GNU 10.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/test/sda3/work/cmake/Step9_build
test@test:~/sda3/work/cmake/Step9_build$
test@test:~/sda3/work/cmake/Step9_build$
test@test:~/sda3/work/cmake/Step9_build$ cmake --build .
[ 11%] Building CXX object MathFunctions/CMakeFiles/MakeTable.dir/MakeTable.cxx.o
[ 22%] Linking CXX executable MakeTable
[ 22%] Built target MakeTable
[ 33%] Generating Table.h
[ 44%] Building CXX object MathFunctions/CMakeFiles/SqrtLibrary.dir/mysqrt.cxx.o
[ 55%] Linking CXX static library libSqrtLibrary.a
[ 55%] Built target SqrtLibrary
[ 66%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/MathFunctions.cxx.o
[ 77%] Linking CXX static library libMathFunctions.a
[ 77%] Built target MathFunctions
[ 88%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cxx.o
[100%] Linking CXX executable Tutorial
[100%] Built target Tutorial
test@test:~/sda3/work/cmake/Step9_build$
test@test:~/sda3/work/cmake/Step9_build$ cpack
CPack: Create package using STGZ
CPack: Install projects
CPack: - Run preinstall target for: Tutorial
CPack: - Install project: Tutorial []
CPack: Create package
CPack: - package: /home/test/sda3/work/cmake/Step9_build/Tutorial-1.0-Linux.sh generated.
CPack: Create package using TGZ
CPack: Install projects
CPack: - Run preinstall target for: Tutorial
CPack: - Install project: Tutorial []
CPack: Create package
CPack: - package: /home/test/sda3/work/cmake/Step9_build/Tutorial-1.0-Linux.tar.gz generated.
CPack: Create package using TZ
CPack: Install projects
CPack: - Run preinstall target for: Tutorial
CPack: - Install project: Tutorial []
CPack: Create package
CPack: - package: /home/test/sda3/work/cmake/Step9_build/Tutorial-1.0-Linux.tar.Z generated.
test@test:~/sda3/work/cmake/Step9_build$ ls -alh
total 148K
drwxrwxr-x 6 test test 4.0K 10月 24 22:03 .
drwxrwxr-x 18 test test 4.0K 10月 24 22:02 ..
-rw-rw-r-- 1 test test 18K 10月 24 22:02 CMakeCache.txt
drwxrwxr-x 33 test test 4.0K 10月 24 22:03 CMakeFiles
-rw-rw-r-- 1 test test 2.8K 10月 24 22:02 cmake_install.cmake
-rw-r--r-- 1 test test 3.2K 10月 24 22:02 CPackConfig.cmake
drwxrwxr-x 3 test test 4.0K 10月 24 22:03 _CPack_Packages
-rw-r--r-- 1 test test 3.6K 10月 24 22:02 CPackSourceConfig.cmake
-rw-rw-r-- 1 test test 3.3K 10月 24 22:02 CTestTestfile.cmake
-rw-r--r-- 1 test test 2.5K 10月 24 22:02 DartConfiguration.tcl
-rw-rw-r-- 1 test test 535 10月 24 22:03 install_manifest.txt
-rw-rw-r-- 1 test test 24K 10月 24 22:02 Makefile
drwxrwxr-x 3 test test 4.0K 10月 24 22:03 MathFunctions
drwxrwxr-x 3 test test 4.0K 10月 24 22:02 Testing
-rwxrwxr-x 1 test test 20K 10月 24 22:03 Tutorial
-rwxrwxrwx 1 test test 11K 10月 24 22:03 Tutorial-1.0-Linux.sh
-rw-rw-r-- 1 test test 6.6K 10月 24 22:03 Tutorial-1.0-Linux.tar.gz
-rw-rw-r-- 1 test test 9.9K 10月 24 22:03 Tutorial-1.0-Linux.tar.Z
-rw-r--r-- 1 test test 118 10月 24 22:02 TutorialConfig.h
test@test:~/sda3/work/cmake/Step9_build$
test@test:~/sda3/work/cmake/Step9_build$ cpack --config CPackSourceConfig.cmake
CPack: Create package using TGZ
CPack: Install projects
CPack: - Install directory: /home/test/sda3/work/cmake/Step9
CPack: Create package
CPack: - package: /home/test/sda3/work/cmake/Step9_build/Tutorial-1.0-Source.tar.gz generated.
test@test:~/sda3/work/cmake/Step9_build$
test@test:~/sda3/work/cmake/Step9_build$ ./Tutorial 100
Computing sqrt of 100 to be 50.5
Computing sqrt of 100 to be 26.2401
Computing sqrt of 100 to be 15.0255
Computing sqrt of 100 to be 10.8404
Computing sqrt of 100 to be 10.0326
Computing sqrt of 100 to be 10.0001
Computing sqrt of 100 to be 10
Computing sqrt of 100 to be 10
Computing sqrt of 100 to be 10
Computing sqrt of 100 to be 10
The square root of 100 is 10
test@test:~/sda3/work/cmake/Step9_build$