CMake 4 (find_package)

CMakeやります。ROS2の為に。

簡単な例を読み解こう

ここのCMakeLists.txtを読み解いていきます。

ROS2 2 : Workspace, Package

CMakeLists.txt


cmake_minimum_required(VERSION 3.8)
project(hello)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

add_executable(hello_node src/hello_node.cpp)
target_include_directories(hello_node PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
target_compile_features(hello_node PUBLIC c_std_99 cxx_std_17)  # Require C99 and C++17

install(TARGETS hello_node
  DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

find_package

公式。
latest release 3.27.6 (2023.09.21)

find_package

REQUIRED

Regardless of the mode used, a <PackageName>_FOUND variable will be set to indicate whether the package was found. When the package is found, package-specific information may be provided through other variables and Imported Targets documented by the package itself. The QUIET option disables informational messages, including those indicating that the package cannot be found if it is not REQUIRED. The REQUIRED option stops processing with an error message if the package cannot be found.

REQUIREDが記述されているとpackageが見つからないときに処理を止める。

動かしてみる


$ cd ~/ros2_study/hello_world_cpp_ws
$ colcon build --packages-select hello --event-handlers console_cohesion+ --cmake-args -D CMAKE_VERBOSE_MAKEFILE=ON

Starting >>> hello
--- output: hello
-- Found ament_cmake: 1.3.4 (/opt/ros/humble/share/ament_cmake/cmake)
-- Found ament_lint_auto: 0.12.6 (/opt/ros/humble/share/ament_lint_auto/cmake)
-- Added test 'cppcheck' to perform static code analysis on C / C++ code
-- Configured cppcheck include dirs: $<BUILD_INTERFACE:/home/jn/ros_study/hello_world_cpp_ws/src/hello/include>
-- Configured cppcheck exclude dirs and/or files: 
-- Added test 'lint_cmake' to check CMake code style
-- Added test 'uncrustify' to check C / C++ code style
-- Configured uncrustify additional arguments: 
-- Added test 'xmllint' to check XML markup files
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jn/ros_study/hello_world_cpp_ws/build/hello
/usr/bin/cmake -S/home/jn/ros_study/hello_world_cpp_ws/src/hello -B/home/jn/ros_study/hello_world_cpp_ws/build/hello --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/jn/ros_study/hello_world_cpp_ws/build/hello/CMakeFiles /home/jn/ros_study/hello_world_cpp_ws/build/hello//CMakeFiles/progress.marks
/usr/bin/gmake  -f CMakeFiles/Makefile2 all
gmake[1]: Entering directory '/home/jn/ros_study/hello_world_cpp_ws/build/hello'
/usr/bin/gmake  -f CMakeFiles/hello_node.dir/build.make CMakeFiles/hello_node.dir/depend
gmake[2]: Entering directory '/home/jn/ros_study/hello_world_cpp_ws/build/hello'
cd /home/jn/ros_study/hello_world_cpp_ws/build/hello && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/jn/ros_study/hello_world_cpp_ws/src/hello /home/jn/ros_study/hello_world_cpp_ws/src/hello /home/jn/ros_study/hello_world_cpp_ws/build/hello /home/jn/ros_study/hello_world_cpp_ws/build/hello /home/jn/ros_study/hello_world_cpp_ws/build/hello/CMakeFiles/hello_node.dir/DependInfo.cmake --color=
Dependencies file "CMakeFiles/hello_node.dir/src/hello_node.cpp.o.d" is newer than depends file "/home/jn/ros_study/hello_world_cpp_ws/build/hello/CMakeFiles/hello_node.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target hello_node
gmake[2]: Leaving directory '/home/jn/ros_study/hello_world_cpp_ws/build/hello'
/usr/bin/gmake  -f CMakeFiles/hello_node.dir/build.make CMakeFiles/hello_node.dir/build
gmake[2]: Entering directory '/home/jn/ros_study/hello_world_cpp_ws/build/hello'
gmake[2]: Nothing to be done for 'CMakeFiles/hello_node.dir/build'.
gmake[2]: Leaving directory '/home/jn/ros_study/hello_world_cpp_ws/build/hello'
[100%] Built target hello_node
gmake[1]: Leaving directory '/home/jn/ros_study/hello_world_cpp_ws/build/hello'
/usr/bin/cmake -E cmake_progress_start /home/jn/ros_study/hello_world_cpp_ws/build/hello/CMakeFiles 0
-- Install configuration: ""
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/lib/hello/hello_node
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/ament_index/resource_index/package_run_dependencies/hello
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/ament_index/resource_index/parent_prefix_path/hello
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/environment/ament_prefix_path.sh
-- Installing: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/environment/ament_prefix_path.dsv
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/environment/path.sh
-- Installing: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/environment/path.dsv
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/local_setup.bash
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/local_setup.sh
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/local_setup.zsh
-- Installing: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/local_setup.dsv
-- Installing: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/package.dsv
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/ament_index/resource_index/packages/hello
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/cmake/helloConfig.cmake
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/cmake/helloConfig-version.cmake
-- Up-to-date: /home/jn/ros_study/hello_world_cpp_ws/install/hello/share/hello/package.xml
---
Finished <<< hello [2.24s]

Summary: 1 package finished [3.97s]

下記と出力されています。


-- Found ament_cmake: 1.3.4 (/opt/ros/humble/share/ament_cmake/cmake)

確かめます。


$ ls /opt/ros/humble/share/ament_cmake/cmake

結果です。


ament_cmakeConfig.cmake
ament_cmakeConfig-version.cmake
ament_cmake_export_dependencies-extras.cmake

std::string str = “ament_cmake” + “Config.cmake”;

find_packageのsearch modesのconfig modeとわかります。

config mode

該当のpackageが.cmakeを提供してくれているときのmode

参考

Using Dependencies Guide

find_package

広告

IT開発関連書とビジネス書が豊富な翔泳社の通販『SEshop』
さくらのレンタルサーバ
ムームードメイン
Oisix(おいしっくす)
らでぃっしゅぼーや
珈琲きゃろっと
エプソムソルト




«       »