{"id":15628269,"url":"https://github.com/chaneyzorn/cmake-tutorial","last_synced_at":"2026-01-25T07:37:23.751Z","repository":{"id":144128560,"uuid":"94592860","full_name":"chaneyzorn/CMake-tutorial","owner":"chaneyzorn","description":"CMake 官方教程----的翻译","archived":false,"fork":false,"pushed_at":"2017-06-17T01:27:41.000Z","size":17,"stargazers_count":329,"open_issues_count":0,"forks_count":79,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-02-04T18:49:57.110Z","etag":null,"topics":["cmake","tutorial"],"latest_commit_sha":null,"homepage":"","language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chaneyzorn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-17T01:23:37.000Z","updated_at":"2025-01-19T17:46:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"56f2f118-dd0d-46cb-9c90-ff8a6b1bbce9","html_url":"https://github.com/chaneyzorn/CMake-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaneyzorn%2FCMake-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaneyzorn%2FCMake-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaneyzorn%2FCMake-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chaneyzorn%2FCMake-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chaneyzorn","download_url":"https://codeload.github.com/chaneyzorn/CMake-tutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246223265,"owners_count":20743158,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cmake","tutorial"],"created_at":"2024-10-03T10:21:43.640Z","updated_at":"2026-01-25T07:37:18.731Z","avatar_url":"https://github.com/chaneyzorn.png","language":"CMake","readme":"# CMake-tutorial([原文](https://cmake.org/cmake-tutorial/))\n\n这份渐进式的教程涵盖了 CMake 帮助处理的一些常见的构建问题。许多议题已经在[《Mastering CMake》](http://www.kitware.com/products/books/CMakeBook.html)中作为独立的话题介绍过，但是了解它们是如何在示例项目中结合在一起的将非常有帮助。你可以在 CMake 源码中的 [Tests/Tutorial](https://gitlab.kitware.com/cmake/cmake/tree/master/Tests/Tutorial) 文件夹找到这份教程，每一步的内容都放置在各自的子文件夹中。\n\n## 一个基本的出发点 (Step1)\n\n最简单的项目是从源代码文件中构建一个可执行文件，CMakeLists.txt 文件仅需要两行，这将作为我们教程的起点，内容如下：\n\n```cmake\ncmake_minimum_required (VERSION 2.6)\nproject (Tutorial)\nadd_executable(Tutorial tutorial.cxx)\n```\n\n文件中的命令支持大写、小写或者混合使用，这个例子中的命令使用小写。tutorial.cxx 用于计算一个数的平方根，源码的第一版非常简单：\n\n```c++\n// 计算一个数的平方根\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \u003cmath.h\u003e\nint main (int argc, char *argv[])\n{\n  if (argc \u003c 2)\n    {\n    fprintf(stdout,\"Usage: %s number\\n\",argv[0]);\n    return 1;\n    }\n  double inputValue = atof(argv[1]);\n  double outputValue = sqrt(inputValue);\n  fprintf(stdout,\"The square root of %g is %g\\n\",\n          inputValue, outputValue);\n  return 0;\n}\n```\n\n### 添加一个版本号并配置头文件\n\n你可以直接在源代码中添加版本号，但在 CMakeLists.txt 文件中提供版本号将会更加灵活，我们将文件修改如下：\n\n```cmake\ncmake_minimum_required (VERSION 2.6)\nproject (Tutorial)\n# 版本号 1.0\nset (Tutorial_VERSION_MAJOR 1)\nset (Tutorial_VERSION_MINOR 0)\n\n# 配置一个头文件将一些 CMake 设置传入到源代码中\n# 以 TutorialConfig.h.in 为模版，替换相关变量\n# 以生成 TutorialConfig.h\nconfigure_file (\n  \"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in\"\n  \"${PROJECT_BINARY_DIR}/TutorialConfig.h\"\n  )\n\n# 将构建目录添加到 include 的搜索路径中以便找到\n# TutorialConfig.h 文件\ninclude_directories(\"${PROJECT_BINARY_DIR}\")\n\n# 添加可执行文件\nadd_executable(Tutorial tutorial.cxx)\n```\n\n因为配置文件将会写入到构建目录中，所以我们将这个目录添加到包含文件的搜索路径中。在源代码中添加 TutorialConfig.h.in 文件：\n\n```c++\n// the configured options and settings for Tutorial\n#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@\n#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@\n```\n\n当 CMake 生成这个头文件时，@Tutorial_VERSION_MAJOR@ 和 @Tutorial_VERSION_MINOR@ 的值将会由 CMakeLists.txt 中对应的值替换。接下来我们将头文件包含到 tutorial.cxx 中并且使用这个版本号，代码如下：\n\n```c++\n// A simple program that computes the square root of a number\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \u003cmath.h\u003e\n#include \"TutorialConfig.h\"\n\nint main (int argc, char *argv[])\n{\n  if (argc \u003c 2)\n    {\n    fprintf(stdout,\"%s Version %d.%d\\n\",\n            argv[0],\n            Tutorial_VERSION_MAJOR,\n            Tutorial_VERSION_MINOR);\n    fprintf(stdout,\"Usage: %s number\\n\",argv[0]);\n    return 1;\n    }\n  double inputValue = atof(argv[1]);\n  double outputValue = sqrt(inputValue);\n  fprintf(stdout,\"The square root of %g is %g\\n\",\n          inputValue, outputValue);\n  return 0;\n}\n```\n\n主要的改变是包含了头文件并且在使用方法信息中打印了版本号。\n\n## 添加一个库 (Step 2)\n\n现在我们要在项目中添加一个库，这个库将会包含我们自己的计算平方根的实现。可执行文件将可以使用这个库，而不是使用编译器提供的平方根标准方法。本教程中将这个库放到名为 MathFunctions 的子文件夹中，这个子文件夹需要包含一个 CMakeLists.txt 文件，文件中有如下一行：\n\n```cmake\nadd_library(MathFunctions mysqrt.cxx)\n```\n\nmysqrt.cxx 文件中有一个叫做 mysqrt 的函数，它提供与编译器的 sqrt 函数相同的功能。我们在顶层的 CMakeLists.txt 中添加一个 add_subdirectory 调用以构建这个库。为了找到 MathFunctions/MathFunctions.h 头文件中的函数原型，我们添加另一条包含路径。最后一个改动是将这个库添加到可执行文件中。顶层 CMakeLists.txt 文件中添加的最新几行如下：\n\n```cmake\ninclude_directories (\"${PROJECT_SOURCE_DIR}/MathFunctions\")\nadd_subdirectory (MathFunctions)\n\n# add the executable\nadd_executable (Tutorial tutorial.cxx)\ntarget_link_libraries (Tutorial MathFunctions)\n```\n\n考虑一下将这个库设计为可选的，本教程中这样做也许是不必要的，但是当使用更大的库或者第三方的库时你也许会用到。第一步是在顶层的 CMakeLists.txt 中添加一个选择：\n\n```cmake\n# 是否使用我们自己的函数？\noption (USE_MYMATH\n        \"Use tutorial provided math implementation\" ON)\n```\n\nCMake GUI 中将会显示一个 ON 的默认值，用户可以按需更改。这个设置将会被缓存，这样在每次对这个项目运行 CMake 时用户不需要再次设置。接下来的更改是将 MathFunctions 库的构建和连接设置为可选的，我们在顶层 CMakeLists.txt 的最后修改如下：\n\n```cmake\n# add the MathFunctions library?\nif (USE_MYMATH)\n  include_directories (\"${PROJECT_SOURCE_DIR}/MathFunctions\")\n  add_subdirectory (MathFunctions)\n  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)\nendif (USE_MYMATH)\n\n# add the executable\nadd_executable (Tutorial tutorial.cxx)\ntarget_link_libraries (Tutorial  ${EXTRA_LIBS})\n```\n\n这将根据 USE_MYMATH 的设置来决定是否编译并使用 MathFunctions 库。注意这里使用了一个 EXTRA_LIBS 变量来收集任何可选的库，以在之后链接到可执行文件中。对有许多可选组件的项目，这是一种保持其整洁的常用方法。相应的源代码更改如下：\n\n```c++\n/ A simple program that computes the square root of a number\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \u003cmath.h\u003e\n#include \"TutorialConfig.h\"\n#ifdef USE_MYMATH\n#include \"MathFunctions.h\"\n#endif\n\nint main (int argc, char *argv[])\n{\n  if (argc \u003c 2)\n    {\n    fprintf(stdout,\"%s Version %d.%d\\n\", argv[0],\n            Tutorial_VERSION_MAJOR,\n            Tutorial_VERSION_MINOR);\n    fprintf(stdout,\"Usage: %s number\\n\",argv[0]);\n    return 1;\n    }\n\n  double inputValue = atof(argv[1]);\n\n#ifdef USE_MYMATH\n  double outputValue = mysqrt(inputValue);\n#else\n  double outputValue = sqrt(inputValue);\n#endif\n\n  fprintf(stdout,\"The square root of %g is %g\\n\",\n          inputValue, outputValue);\n  return 0;\n}\n```\n\n在源代码中我们同样使用了 USE_MYMATH 变量。通过在 TutorialConfig.h.in 中添加如下配置，Cmake 将这个变量引入到源代码中：\n\n```c++\n#cmakedefine USE_MYMATH\n```\n\n## 安装与测试 (Step 3)\n\n接下来我们在项目中添加安装规则和测试支持。安装规则非常直接，对于 MathFunctions 库的安装，我们在  MathFunctions 的 CMakeLists.txt 中添加如下几行：\n\n```cmake\ninstall (TARGETS MathFunctions DESTINATION bin)\ninstall (FILES MathFunctions.h DESTINATION include)\n```\n\n对于应用程序可执行文件和头文件的安装，我们在顶层的 CMakeLists.txt 中添加如下几行：\n\n```cmake\n# add the install targets\ninstall (TARGETS Tutorial DESTINATION bin)\ninstall (FILES \"${PROJECT_BINARY_DIR}/TutorialConfig.h\"\n         DESTINATION include)\n```\n\n万事俱备，接下来你应该可以构建这个项目，然后键入 `make install` （或者在 IDE 中构建 INSTALL 目标），它将会安装合适的头文件，库和执行文件。CMake 的 CMAKE_INSTALL_PREFIX 变量用于决定文件安装位置的根。添加测试也是一个同样直接的过程。在顶层 CMakeLists.txt 的结尾，我们可以添加几个基础测试以判别程序是否工作正常：\n\n```camke\ninclude(CTest)\n\n# does the application run\nadd_test (TutorialRuns Tutorial 25)\n\n# does it sqrt of 25\nadd_test (TutorialComp25 Tutorial 25)\nset_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION \"25 is 5\")\n\n# does it handle negative numbers\nadd_test (TutorialNegative Tutorial -25)\nset_tests_properties (TutorialNegative PROPERTIES PASS_REGULAR_EXPRESSION \"-25 is 0\")\n\n# does it handle small numbers\nadd_test (TutorialSmall Tutorial 0.0001)\nset_tests_properties (TutorialSmall PROPERTIES PASS_REGULAR_EXPRESSION \"0.0001 is 0.01\")\n\n# does the usage message work?\nadd_test (TutorialUsage Tutorial)\nset_tests_properties (TutorialUsage PROPERTIES PASS_REGULAR_EXPRESSION \"Usage:.*number\")\n```\n\n构建完成后，可以使用命令行工具 `ctest` 运行测试。第一个测试只是验证程序是否运行，没有段错误或其他的崩溃，并返回零值。这是一个 CTest 测试的基本形式。接下来的几个测试都使用 PASS_REGULAR_EXPRESSION 测试属性来验证测试的输出是否包含某些字符串。这样用来验证预期的计算结果，并且当参数数目不正确时打印使用信息。如果你想添加大量测试来测试不同的输入值，你可能会考虑创建如下所示的宏：\n\n```cmake\n#define a macro to simplify adding tests, then use it\nmacro (do_test arg result)\n  add_test (TutorialComp${arg} Tutorial ${arg})\n  set_tests_properties (TutorialComp${arg}\n    PROPERTIES PASS_REGULAR_EXPRESSION ${result})\nendmacro (do_test)\n\n# do a bunch of result based tests\ndo_test (25 \"25 is 5\")\ndo_test (-25 \"-25 is 0\")\n```\n\n每调用一次 do_test，根据传递的参数，都会添加一个拥有名字、输入和输出的测试。\n\n## 添加系统自检 (Step 4)\n\n接下来让我们向项目中添加一些代码，这些代码依赖的功能目标平台可能没有提供。这个例子中，我们添加的代码依赖于目标平台是否提供了对数 log 和指数 exp 函数。当然几乎所有的平台都提供了这样的函数，本教程假定它们是不常见的功能。如果平台提供了 log，那么我们可以在 mysqrt 函数中使用它计算平方根。我们首先使用顶层 CMakeLists.txt 文件中的 CheckFunctionExists.cmake 宏来测试这些功能的可用性，如下所示：\n\n```cmake\n# does this system provide the log and exp functions?\ninclude (CheckFunctionExists)\ncheck_function_exists (log HAVE_LOG)\ncheck_function_exists (exp HAVE_EXP)\n```\n\n当 CMake 在平台上发现它们时，我们在 TutorialConfig.h.in 中定义这些值：\n\n```cmake\n// does the platform provide exp and log functions?\n#cmakedefine HAVE_LOG\n#cmakedefine HAVE_EXP\n```\n\nlog 和 exp 的测试需要放在 configure_file 命令之前，configure_file 命令会立即使用 CMake 中的当前设置生成文件。当系统提供了这两个函数时，我们可以使用以下代码在 mysqrt 函数中提供一个基于 log 和 exp 的替代实现：\n\n```c++\n// if we have both log and exp then use them\n#if defined (HAVE_LOG) \u0026\u0026 defined (HAVE_EXP)\n  result = exp(log(x)*0.5);\n#else // otherwise use an iterative approach\n  . . .\n```\n\n## 添加生成文件和生成器 (Step 5)\n\n在本节中，我们将展示如何将生成的源文件添加到应用程序的构建过程中。本例中，我们将会在构建过程中创建一个预先计算的平方根表，然后将这张表编译进我们的程序中。我们首先需要一个生成这张表的程序，为此我们在 MathFunctions 的子文件夹中添加一个新的名为 MakeTable.cxx 的源文件：\n\n```c++\n// A simple program that builds a sqrt table\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \u003cmath.h\u003e\n\nint main (int argc, char *argv[])\n{\n  int i;\n  double result;\n\n  // make sure we have enough arguments\n  if (argc \u003c 2)\n    {\n    return 1;\n    }\n\n  // open the output file\n  FILE *fout = fopen(argv[1],\"w\");\n  if (!fout)\n    {\n    return 1;\n    }\n\n  // create a source file with a table of square roots\n  fprintf(fout,\"double sqrtTable[] = {\\n\");\n  for (i = 0; i \u003c 10; ++i)\n    {\n    result = sqrt(static_cast\u003cdouble\u003e(i));\n    fprintf(fout,\"%g,\\n\",result);\n    }\n\n  // close the table with a zero\n  fprintf(fout,\"0};\\n\");\n  fclose(fout);\n  return 0;\n}\n```\n\n注意这张表会以有效的 C++ 代码的形式生成，输出文件的名字以参数的形式提供。接下来向 MathFunctions 的 CMakeLists.txt 文件中添加合适的命令以构建 MakeTable 的可执行文件，并运行它作为构建过程的一部分。需要几个命令来完成此操作，如下所示：\n\n```cmake\n# first we add the executable that generates the table\nadd_executable(MakeTable MakeTable.cxx)\n\n# add the command to generate the source code\nadd_custom_command (\n  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h\n  COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h\n  DEPENDS MakeTable\n  )\n\n# add the binary tree directory to the search path for\n# include files\ninclude_directories( ${CMAKE_CURRENT_BINARY_DIR} )\n\n# add the main library\nadd_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h  )\n```\n\n首先像添加其他执行文件那样添加 MakeTable 的执行文件。然后我们添加一个自定义命令以使用 MakeTable 生成 Table.h 文件。接下来我们需要将生成的文件添加到 MathFunctions 库的源文件列表中，以让 CMake 知道 mysqrt.cxx 依赖于 Table.h 文件。我们也需要将当前的构建文件夹添加到包含文件列表中，以让 Table.h 文件可以被发现并包含到 mysqrt.cxx 中。当构建这个项目时，它会先构建 MakeTable 的执行文件，然后运行 MakeTable 生成 Table.h 文件，最后它会编译包含有 Table.h 的 mysqrt.cxx 以生成 MathFunctions 库。此时，顶层 CMakeLists.txt 文件如下所示：\n\n```cmake\ncmake_minimum_required (VERSION 2.6)\nproject (Tutorial)\ninclude(CTest)\n\n# The version number.\nset (Tutorial_VERSION_MAJOR 1)\nset (Tutorial_VERSION_MINOR 0)\n\n# does this system provide the log and exp functions?\ninclude (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)\n\ncheck_function_exists (log HAVE_LOG)\ncheck_function_exists (exp HAVE_EXP)\n\n# should we use our own math functions\noption(USE_MYMATH\n  \"Use tutorial provided math implementation\" ON)\n\n# configure a header file to pass some of the CMake settings\n# to the source code\nconfigure_file (\n  \"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in\"\n  \"${PROJECT_BINARY_DIR}/TutorialConfig.h\"\n  )\n\n# add the binary tree to the search path for include files\n# so that we will find TutorialConfig.h\ninclude_directories (\"${PROJECT_BINARY_DIR}\")\n\n# add the MathFunctions library?\nif (USE_MYMATH)\n  include_directories (\"${PROJECT_SOURCE_DIR}/MathFunctions\")\n  add_subdirectory (MathFunctions)\n  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)\nendif (USE_MYMATH)\n\n# add the executable\nadd_executable (Tutorial tutorial.cxx)\ntarget_link_libraries (Tutorial  ${EXTRA_LIBS})\n\n# add the install targets\ninstall (TARGETS Tutorial DESTINATION bin)\ninstall (FILES \"${PROJECT_BINARY_DIR}/TutorialConfig.h\"\n         DESTINATION include)\n\n# does the application run\nadd_test (TutorialRuns Tutorial 25)\n\n# does the usage message work?\nadd_test (TutorialUsage Tutorial)\nset_tests_properties (TutorialUsage\n  PROPERTIES\n  PASS_REGULAR_EXPRESSION \"Usage:.*number\"\n  )\n\n\n#define a macro to simplify adding tests\nmacro (do_test arg result)\n  add_test (TutorialComp${arg} Tutorial ${arg})\n  set_tests_properties (TutorialComp${arg}\n    PROPERTIES PASS_REGULAR_EXPRESSION ${result}\n    )\nendmacro (do_test)\n\n# do a bunch of result based tests\ndo_test (4 \"4 is 2\")\ndo_test (9 \"9 is 3\")\ndo_test (5 \"5 is 2.236\")\ndo_test (7 \"7 is 2.645\")\ndo_test (25 \"25 is 5\")\ndo_test (-25 \"-25 is 0\")\ndo_test (0.0001 \"0.0001 is 0.01\")\n```\n\nTutorialConfig.h.in 如下：\n\n```c++\n// the configured options and settings for Tutorial\n#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@\n#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@\n#cmakedefine USE_MYMATH\n\n// does the platform provide exp and log functions?\n#cmakedefine HAVE_LOG\n#cmakedefine HAVE_EXP\n```\n\nMathFunctions 的 CMakeLists.txt 文件如下：\n\n```cmake\n# first we add the executable that generates the table\nadd_executable(MakeTable MakeTable.cxx)\n# add the command to generate the source code\nadd_custom_command (\n  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h\n  DEPENDS MakeTable\n  COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h\n  )\n# add the binary tree directory to the search path\n# for include files\ninclude_directories( ${CMAKE_CURRENT_BINARY_DIR} )\n\n# add the main library\nadd_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h)\n\ninstall (TARGETS MathFunctions DESTINATION bin)\ninstall (FILES MathFunctions.h DESTINATION include)\n```\n\n## 构建安装程序 (Step 6)\n\n接下来假设我们想将我们的项目分发给其他人，以便他们可以使用它。我们希望在各种平台上提供二进制和源代码分发。这与之前的第三步有些不同，在这个例子中，我们将构建安装包以支持二进制安装和包管理功能，比如 cygwin，debian，RPMs 等。我们将会使用 CPack 来创建平台相关的安装程序。具体来说，我们需要在我们的顶层 CMakeLists.txt 文件的底部添加几行：\n\n```cmake\n# build a CPack driven installer package\ninclude (InstallRequiredSystemLibraries)\nset (CPACK_RESOURCE_FILE_LICENSE\n     \"${CMAKE_CURRENT_SOURCE_DIR}/License.txt\")\nset (CPACK_PACKAGE_VERSION_MAJOR \"${Tutorial_VERSION_MAJOR}\")\nset (CPACK_PACKAGE_VERSION_MINOR \"${Tutorial_VERSION_MINOR}\")\ninclude (CPack)\n```\n\n我们从包含 InstallRequiredSystemLibraries 开始。该模块包含有这个项目在当前平台所需的任何运行时库。然后我们设置一些 CPack 变量指明此项目许可证和版本信息的位置。版本信息使用了本教程前面设置的变量。最后我们包含了 CPack 模块，它将使用你设置的这些变量和其他系统属性来配置安装程序。\n\n接下来就是按照通常的方法构建项目，然后运行 CPack 命令。要构建一个二进制分发，你可以运行：\n\n```bash\ncpack --config CPackConfig.cmake\n```\n\n要创建一个源码分发，你可以键入：\n\n```bash\ncpack --config CPackSourceConfig.cmake\n```\n\n## 添加对仪表板的支持 (Step 7)\n\n添加将测试结果提交给仪表板的支持非常简单。在教程之前的步骤中已经定义了一些测试，我们只需运行这些测试并且将它们提交给一个仪表板。要包括对仪表板的支持，我们将 CTest 模块包含在我们的顶层 CMakeLists.txt 文件中：\n\n```cmake\n# enable dashboard scripting\ninclude (CTest)\n```\n\n我们还创建一个CTestConfig.cmake文件，可以在该文件中为仪表板指定此项目的名称。\n\n```cmake\nset (CTEST_PROJECT_NAME \"Tutorial\")\n```\n\n当运行 CTest 时它会读取这个文件。要创建简单的仪表板，你可以在项目中运行 CMake，然后切换目录到构建目录中运行 `ctest –D Experimental`. 仪表板的结构将会上传到 Kitware 的公共仪表板中（[这里](http://www.cdash.org/CDash/index.php?project=PublicDashboard)）。","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchaneyzorn%2Fcmake-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchaneyzorn%2Fcmake-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchaneyzorn%2Fcmake-tutorial/lists"}