CMake官方教程中文翻译 Step 5: Installing and Testing

鉴于自己破烂的英语,所以把cmake的官方文档用 谷歌翻译 翻译下来方便查看。
英语好的同学建议直接去看cmake官方文档(英文)学习:地址 点这里
或复制:https://cmake.org/cmake/help/latest/guide/tutorial/index.html

因为官方文档有点多,所以只截取CMake Tutorial的 step1——step12 进行翻译,这是第5步的翻译,以下是每一步的翻译链接:
Documentation » CMake Tutorial » Step 1: A Basic Starting Point
Documentation » CMake Tutorial » Step 2: Adding a Library
Documentation » CMake Tutorial » Step 3: Adding Usage Requirements for a Library
Documentation » CMake Tutorial » Step 4: Adding Generator Expressions
[Documentation » CMake Tutorial » Step 5: Installing and Testing]
Documentation » CMake Tutorial » Step 6: Adding Support for a Testing Dashboard
Documentation » CMake Tutorial » Step 7: Adding System Introspection
Documentation » CMake Tutorial » Step 8: Adding a Custom Command and Generated File
Documentation » CMake Tutorial » Step 9: Packaging an Installer
Documentation » CMake Tutorial » Step 10: Selecting Static or Shared Libraries
Documentation » CMake Tutorial » Step 11: Adding Export Configuration
Documentation » CMake Tutorial » Step 12: Packaging Debug and Release
谷歌翻译可能有错,此文档的目的仅是加快观看英文官方文档的速度,所以请结合英文官方文档观看

Step 5: Installing and Testing

Exercise 1 - Install Rules

通常,仅构建可执行文件是不够的,它还应该是可安装的。 使用 CMake,我们可以使用 install() 命令指定安装规则。 在 CMake 中支持本地安装通常非常简单,只需指定安装位置以及要安装的目标和文件即可。

Goal

安装Tutorial可执行文件和 MathFunctions 库。

Helpful Materials

install()

Files to Edit

MathFunctions/CMakeLists.txt
CMakeLists.txt

Getting Started

Step5 目录中提供了起始代码。 在此练习中,完成 TODO 1 到 TODO 4。

首先,更新 MathFunctions/CMakeLists.txt 以将 MathFunctions 和tutorial_compiler_flags 库安装到 lib 目录。 在同一文件中,指定将 MathFunctions.h 安装到包含目录所需的安装规则。

然后,更新顶级 CMakeLists.txt 以将教程可执行文件安装到 bin 目录。 最后,所有头文件都应安装到包含目录中。 请记住,TutorialConfig.h 位于 PROJECT_BINARY_DIR 中。

Build and Run

创建一个名为 Step5_build 的新目录。 运行 cmake 可执行文件或 cmake-gui 来配置项目,然后使用您选择的构建工具进行构建。

然后,从命令行使用 cmake 命令的 --install 选项(在 3.15 中引入,旧版本的 CMake 必须使用 make install)运行安装步骤。 此步骤将安装适当的头文件、库和可执行文件。 例如:
cmake --install .

对于多配置工具,不要忘记使用 --config 参数来指定配置。
cmake --install . --config Release

如果使用 IDE,只需构建 INSTALL 目标即可。 您可以从命令行构建相同的安装目标,如下所示:
cmake --build . --target install --config Debug

CMake 变量 CMAKE_INSTALL_PREFIX 用于确定文件安装位置的根目录。 如果使用 cmake --install 命令,则可以通过 --prefix 参数覆盖安装前缀。 例如:
cmake --install . --prefix “/home/myuser/installdir”

导航到安装目录并验证已安装的教程是否运行。

Solution

我们项目的安装规则非常简单:

对于 MathFunctions,我们希望将库和头文件分别安装到 lib 和 include 目录。

对于教程可执行文件,我们希望将可执行文件和配置的头文件分别安装到 bin 和 include 目录。

因此,在 MathFunctions/CMakeLists.txt 的末尾我们添加:

TODO 1: MathFunctions/CMakeLists.txt
set(installable_libs MathFunctions tutorial_compiler_flags)
if(TARGET SqrtLibrary)
list(APPEND installable_libs SqrtLibrary)
endif()
install(TARGETS ${installable_libs} DESTINATION lib)

和:

TODO 2: MathFunctions/CMakeLists.txt
install(FILES MathFunctions.h DESTINATION include)

教程可执行文件和配置的头文件的安装规则类似。 在顶级 CMakeLists.txt 的末尾,我们添加:

TODO 3,4: CMakeLists.txt
install(TARGETS Tutorial DESTINATION bin)
install(FILES “${PROJECT_BINARY_DIR}/TutorialConfig.h”
DESTINATION include
)

这就是创建本教程的基本本地安装所需的全部内容。

Exercise 2 - Testing Support

CTest 提供了一种轻松管理项目测试的方法。 可以通过 add_test() 命令添加测试。 尽管本教程中没有明确介绍,但 CTest 和其他测试框架(例如 GoogleTest)之间存在很多兼容性。

Goal

使用 CTest 为我们的可执行文件创建单元测试。

Helpful Materials

enable_testing()
add_test()
function()
set_tests_properties()
ctest

Files to Edit

CMakeLists.txt

Getting Started

Step5 目录中提供了起始源代码。 在此练习中,完成 TODO 5 到 TODO 9。

首先,我们需要启用测试。 接下来,开始使用 add_test() 将测试添加到我们的项目中。 我们将添加 3 个简单的测试,然后您可以根据需要添加其他测试。

Build and Run

导航到构建目录并重建应用程序。 然后,运行 ctest 可执行文件:ctest -N 和 ctest -VV。 对于多配置生成器(例如 Visual Studio),必须使用 -C <mode> 标志指定配置类型。 例如,要在调试模式下运行测试,请使用构建目录(而不是调试子目录!)中的 ctest -C Debug -VV。 发布模式将从相同位置执行,但使用 -C Release。 或者,从 IDE 构建 RUN_TESTS 目标。

Solution

让我们测试一下我们的应用程序。 在顶级 CMakeLists.txt 文件的末尾,我们首先需要使用 enable_testing() 命令启用测试。

TODO 5: CMakeLists.txt
enable_testing()

启用测试后,我们将添加一些基本测试来验证应用程序是否正常工作。 首先,我们使用 add_test() 创建一个测试,该测试运行传入参数 25 的教程可执行文件。对于此测试,我们不会检查可执行文件的计算结果。 此测试将验证应用程序是否运行、没有段错误或以其他方式崩溃,并且返回值为零。 这是 CTest 测试的基本形式。

TODO 6: CMakeLists.txt
add_test(NAME Runs COMMAND Tutorial 25)

接下来,让我们使用 PASS_REGULAR_EXPRESSION 测试属性来验证测试的输出是否包含某些字符串。 在这种情况下,验证当提供的参数数量不正确时是否打印使用消息。

TODO 7: CMakeLists.txt
add_test(NAME Usage COMMAND Tutorial)
set_tests_properties(Usage
PROPERTIES PASS_REGULAR_EXPRESSION “Usage:.*number”
)

我们将添加的下一个测试验证计算值是否确实是平方根。

TODO 8: CMakeLists.txt
add_test(NAME StandardUse COMMAND Tutorial 4)
set_tests_properties(StandardUse
PROPERTIES PASS_REGULAR_EXPRESSION “4 is 2”
)

这一测试不足以让我们相信它适用于传入的所有值。我们应该添加更多测试来验证这一点。 为了轻松添加更多测试,我们创建了一个名为 do_test 的函数,该函数运行应用程序并验证计算出的平方根对于给定输入是否正确。 对于 do_test 的每次调用,都会将另一个测试添加到项目中,并根据传递的参数提供名称、输入和预期结果。

TODO 9: CMakeLists.txt
function(do_test target arg result)
add_test(NAME Comp${arg} COMMAND ${target} a r g ) s e t t e s t s p r o p e r t i e s ( C o m p {arg}) set_tests_properties(Comp arg)settestsproperties(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”)

  • 13
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值