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

本文档介绍了如何使用CMake的install()命令进行项目安装,并配合CTest进行测试。步骤包括设置安装规则、添加测试和支持,以确保可执行文件和库的正确安装及功能测试。
部署运行你感兴趣的模型镜像

鉴于自己破烂的英语,所以把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”)

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

shr@ubuntu:~/h_ws$ catkin_make Base path: /home/shr/h_ws Source space: /home/shr/h_ws/src Build space: /home/shr/h_ws/build Devel space: /home/shr/h_ws/devel Install space: /home/shr/h_ws/install #### #### Running command: "cmake /home/shr/h_ws/src -DCATKIN_DEVEL_PREFIX=/home/shr/h_ws/devel -DCMAKE_INSTALL_PREFIX=/home/shr/h_ws/install -G Unix Makefiles" in "/home/shr/h_ws/build" #### -- Using CATKIN_DEVEL_PREFIX: /home/shr/h_ws/devel -- Using CMAKE_PREFIX_PATH: /opt/ros/noetic -- This workspace overlays: /opt/ros/noetic -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.8.10", minimum required is "3") -- Using PYTHON_EXECUTABLE: /usr/bin/python3 -- Using Debian Python package layout -- Using empy: /usr/lib/python3/dist-packages/em.py -- Using CATKIN_ENABLE_TESTING: ON -- Call enable_testing() -- Using CATKIN_TEST_RESULTS_DIR: /home/shr/h_ws/build/test_results -- Forcing gtest/gmock from source, though one was otherwise available. -- Found gtest sources under '/usr/src/googletest': gtests will be built -- Found gmock sources under '/usr/src/googletest': gmock will be built -- Found PythonInterp: /usr/bin/python3 (found version "3.8.10") -- Using Python nosetests: /usr/bin/nosetests3 -- catkin 0.8.12 -- BUILD_SHARED_LIBS is on -- BUILD_SHARED_LIBS is on -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- ~~ traversing 22 packages in topological order: -- ~~ - arbotix (metapackage) -- ~~ - arbotix_controllers -- ~~ - arbotix_firmware -- ~~ - arbotix_python -- ~~ - arbotix_sensors -- ~~ - plumbing_my (metapackage) -- ~~ - arbotix_msgs -- ~~ - d3_plug -- ~~ - hello_vscode -- ~~ - plumbing_server_client -- ~~ - plumbing_test -- ~~ - pluming_pub_sub -- ~~ - d1_action -- ~~ - d2_dr -- ~~ - rosbag_d -- ~~ - tf2_static -- ~~ - tf3_tfs -- ~~ - t2_dynamic -- ~~ - tf4_test -- ~~ - nav1_demo -- ~~ - urdf1_rviz -- ~~ - urdf2_gazebo -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- +++ processing catkin metapackage: 'arbotix' -- ==> add_subdirectory(arbotix_ros/arbotix) -- +++ processing catkin package: 'arbotix_controllers' -- ==> add_subdirectory(arbotix_ros/arbotix_controllers) -- +++ processing catkin package: 'arbotix_firmware' -- ==> add_subdirectory(arbotix_ros/arbotix_firmware) -- +++ processing catkin package: 'arbotix_python' -- ==> add_subdirectory(arbotix_ros/arbotix_python) -- +++ processing catkin package: 'arbotix_sensors' -- ==> add_subdirectory(arbotix_ros/arbotix_sensors) -- +++ processing catkin metapackage: 'plumbing_my' -- ==> add_subdirectory(plumbing_my) -- +++ processing catkin package: 'arbotix_msgs' -- ==> add_subdirectory(arbotix_ros/arbotix_msgs) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy -- arbotix_msgs: 2 messages, 4 services -- +++ processing catkin package: 'd3_plug' -- ==> add_subdirectory(d3_plug) -- +++ processing catkin package: 'hello_vscode' -- ==> add_subdirectory(hello_vscode) -- Installing devel-space wrapper /home/shr/h_ws/src/hello_vscode/scripts/h1_p.py to /home/shr/h_ws/devel/lib/hello_vscode -- +++ processing catkin package: 'plumbing_server_client' -- ==> add_subdirectory(plumbing_server_client) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy -- plumbing_server_client: 0 messages, 1 services -- Installing devel-space wrapper /home/shr/h_ws/src/plumbing_server_client/scripts/d_server_p.py to /home/shr/h_ws/devel/lib/plumbing_server_client -- Installing devel-space wrapper /home/shr/h_ws/src/plumbing_server_client/scripts/d_client_p.py to /home/shr/h_ws/devel/lib/plumbing_server_client -- +++ processing catkin package: 'plumbing_test' -- ==> add_subdirectory(plumbing_test) -- Installing devel-space wrapper /home/shr/h_ws/src/plumbing_test/scripts/t1_pub_twist_p.py to /home/shr/h_ws/devel/lib/plumbing_test -- Installing devel-space wrapper /home/shr/h_ws/src/plumbing_test/scripts/t2_sub_pose_p.py to /home/shr/h_ws/devel/lib/plumbing_test -- Installing devel-space wrapper /home/shr/h_ws/src/plumbing_test/scripts/t3_server_client_p.py to /home/shr/h_ws/devel/lib/plumbing_test -- +++ processing catkin package: 'pluming_pub_sub' -- ==> add_subdirectory(pluming_pub_sub) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy -- pluming_pub_sub: 1 messages, 0 services -- Installing devel-space wrapper /home/shr/h_ws/src/pluming_pub_sub/scripts/d1_pub_p.py to /home/shr/h_ws/devel/lib/pluming_pub_sub -- Installing devel-space wrapper /home/shr/h_ws/src/pluming_pub_sub/scripts/d1_sub_p.py to /home/shr/h_ws/devel/lib/pluming_pub_sub -- Installing devel-space wrapper /home/shr/h_ws/src/pluming_pub_sub/scripts/d_pub_person_p.py to /home/shr/h_ws/devel/lib/pluming_pub_sub -- Installing devel-space wrapper /home/shr/h_ws/src/pluming_pub_sub/scripts/d_sub_person_p.py to /home/shr/h_ws/devel/lib/pluming_pub_sub -- Installing devel-space wrapper /home/shr/h_ws/src/pluming_pub_sub/scripts/tools.py to /home/shr/h_ws/devel/lib/pluming_pub_sub -- +++ processing catkin package: 'd1_action' -- ==> add_subdirectory(d1_action) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy -- Generating .msg files for action d1_action/Addint /home/shr/h_ws/src/d1_action/action/Addint.action -- d1_action: 7 messages, 0 services CMake Warning at /opt/ros/noetic/share/catkin/cmake/catkin_package.cmake:166 (message): catkin_package() DEPENDS on 'system_lib' but neither 'system_lib_INCLUDE_DIRS' nor 'system_lib_LIBRARIES' is defined. Call Stack (most recent call first): /opt/ros/noetic/share/catkin/cmake/catkin_package.cmake:102 (_catkin_package) d1_action/CMakeLists.txt:106 (catkin_package) -- Installing devel-space wrapper /home/shr/h_ws/src/d1_action/scripts/action1_server_p.py to /home/shr/h_ws/devel/lib/d1_action -- Installing devel-space wrapper /home/shr/h_ws/src/d1_action/scripts/action2_client_p.py to /home/shr/h_ws/devel/lib/d1_action -- +++ processing catkin package: 'd2_dr' -- ==> add_subdirectory(d2_dr) -- Installing devel-space wrapper /home/shr/h_ws/src/d2_dr/scripts/dr1_server_p.py to /home/shr/h_ws/devel/lib/d2_dr -- +++ processing catkin package: 'rosbag_d' -- ==> add_subdirectory(rosbag_d) -- Installing devel-space wrapper /home/shr/h_ws/src/rosbag_d/scripts/d1_write_bag_p.py to /home/shr/h_ws/devel/lib/rosbag_d -- Installing devel-space wrapper /home/shr/h_ws/src/rosbag_d/scripts/d1_read_bag_p.py to /home/shr/h_ws/devel/lib/rosbag_d -- +++ processing catkin package: 'tf2_static' -- ==> add_subdirectory(tf2_static) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy CMake Warning at /home/shr/h_ws/build/tf2_static/cmake/tf2_static-genmsg.cmake:3 (message): Invoking generate_messages() without having added any message or service file before. You should either add add_message_files() and/or add_service_files() calls or remove the invocation of generate_messages(). Call Stack (most recent call first): /opt/ros/noetic/share/genmsg/cmake/genmsg-extras.cmake:307 (include) tf2_static/CMakeLists.txt:75 (generate_messages) -- tf2_static: 0 messages, 0 services -- Installing devel-space wrapper /home/shr/h_ws/src/tf2_static/scripts/t1_static_pub_p.py to /home/shr/h_ws/devel/lib/tf2_static -- Installing devel-space wrapper /home/shr/h_ws/src/tf2_static/scripts/t1_static_sub_p.py to /home/shr/h_ws/devel/lib/tf2_static -- +++ processing catkin package: 'tf3_tfs' -- ==> add_subdirectory(tf3_tfs) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy CMake Warning at /home/shr/h_ws/build/tf3_tfs/cmake/tf3_tfs-genmsg.cmake:3 (message): Invoking generate_messages() without having added any message or service file before. You should either add add_message_files() and/or add_service_files() calls or remove the invocation of generate_messages(). Call Stack (most recent call first): /opt/ros/noetic/share/genmsg/cmake/genmsg-extras.cmake:307 (include) tf3_tfs/CMakeLists.txt:75 (generate_messages) -- tf3_tfs: 0 messages, 0 services -- Installing devel-space wrapper /home/shr/h_ws/src/tf3_tfs/scripts/d1_tfs_sub_p.py to /home/shr/h_ws/devel/lib/tf3_tfs -- +++ processing catkin package: 't2_dynamic' -- ==> add_subdirectory(tf2_dynamic) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy CMake Warning at /home/shr/h_ws/build/tf2_dynamic/cmake/t2_dynamic-genmsg.cmake:3 (message): Invoking generate_messages() without having added any message or service file before. You should either add add_message_files() and/or add_service_files() calls or remove the invocation of generate_messages(). Call Stack (most recent call first): /opt/ros/noetic/share/genmsg/cmake/genmsg-extras.cmake:307 (include) tf2_dynamic/CMakeLists.txt:76 (generate_messages) -- t2_dynamic: 0 messages, 0 services -- Installing devel-space wrapper /home/shr/h_ws/src/tf2_dynamic/scripts/d1_dynamic_pub_p.py to /home/shr/h_ws/devel/lib/t2_dynamic -- Installing devel-space wrapper /home/shr/h_ws/src/tf2_dynamic/scripts/d1_dynamic_sub_p.py to /home/shr/h_ws/devel/lib/t2_dynamic -- +++ processing catkin package: 'tf4_test' -- ==> add_subdirectory(tf4_test) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy CMake Warning at /home/shr/h_ws/build/tf4_test/cmake/tf4_test-genmsg.cmake:3 (message): Invoking generate_messages() without having added any message or service file before. You should either add add_message_files() and/or add_service_files() calls or remove the invocation of generate_messages(). Call Stack (most recent call first): /opt/ros/noetic/share/genmsg/cmake/genmsg-extras.cmake:307 (include) tf4_test/CMakeLists.txt:76 (generate_messages) -- tf4_test: 0 messages, 0 services -- Installing devel-space wrapper /home/shr/h_ws/src/tf4_test/scripts/t1_new_turtle_p.py to /home/shr/h_ws/devel/lib/tf4_test -- Installing devel-space wrapper /home/shr/h_ws/src/tf4_test/scripts/t1_pub_turtle_p.py to /home/shr/h_ws/devel/lib/tf4_test -- Installing devel-space wrapper /home/shr/h_ws/src/tf4_test/scripts/t1_sub_turtle_p.py to /home/shr/h_ws/devel/lib/tf4_test -- +++ processing catkin package: 'nav1_demo' -- ==> add_subdirectory(nav1_demo) -- +++ processing catkin package: 'urdf1_rviz' -- ==> add_subdirectory(urdf1_rviz) -- +++ processing catkin package: 'urdf2_gazebo' -- ==> add_subdirectory(urdf2_gazebo) WARNING: package 'gazebo_plugins' is deprecated (This package has been deprecated as of January 2025 with Gazebo classic 11 reaching end-of-life. Users are highly encouraged to migrate to the new Gazebo using our migration guides (https://gazebosim.org/docs/latest/gazebo_classic_migration)) WARNING: package 'gazebo_msgs' is deprecated (This package has been deprecated as of January 2025 with Gazebo classic 11 reaching end-of-life. Users are highly encouraged to migrate to the new Gazebo using our migration guides (https://gazebosim.org/docs/latest/gazebo_classic_migration)) -- Using these message generators: gencpp;geneus;genlisp;gennodejs;genpy WARNING: package 'gazebo_ros' is deprecated (This package has been deprecated as of January 2025 with Gazebo classic 11 reaching end-of-life. Users are highly encouraged to migrate to the new Gazebo using our migration guides (https://gazebosim.org/docs/latest/gazebo_classic_migration)) WARNING: package 'gazebo_ros_control' is deprecated (This package has been deprecated as of January 2025 with Gazebo classic 11 reaching end-of-life. Users are highly encouraged to migrate to the new Gazebo using our migration guides (https://gazebosim.org/docs/latest/gazebo_classic_migration)) -- Configuring done CMake Error at d3_plug/CMakeLists.txt:123 (add_library): Cannot find source file: src/plug.cpp Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx CMake Error at d3_plug/CMakeLists.txt:123 (add_library): No SOURCES given to target: plus CMake Generate step failed. Build files cannot be regenerated correctly. Invoking "cmake" failed
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值