diff --git a/examples/generating_custom_msgs/CMakeLists.txt b/examples/generating_custom_msgs/CMakeLists.txt index 5aad3e6f..16aed9f9 100644 --- a/examples/generating_custom_msgs/CMakeLists.txt +++ b/examples/generating_custom_msgs/CMakeLists.txt @@ -11,15 +11,17 @@ project(generating_custom_messages VERSION 1.0.0) find_package(gz-cmake3 REQUIRED) find_package(gz-msgs10 REQUIRED) +# Define a variable 'GZ_MSGS_VER' holding the version number set(GZ_MSGS_VER ${gz-msgs10_VERSION_MAJOR}) -# Example of custom messages that depend on gz.msgs +# Define a variable 'MSGS_PROTOS' listing the .proto files set(MSGS_PROTOS ${CMAKE_CURRENT_SOURCE_DIR}/proto/gz/custom_msgs/foo.proto ${CMAKE_CURRENT_SOURCE_DIR}/proto/gz/custom_msgs/bar.proto ${CMAKE_CURRENT_SOURCE_DIR}/proto/gz/custom_msgs/baz.proto ) +# Call 'gz_msgs_generate_messages()' to process the .proto files gz_msgs_generate_messages( # The cmake target to be generated for libraries/executables to link TARGET msgs @@ -28,8 +30,9 @@ gz_msgs_generate_messages( # The path to the base directory of the proto files # All import paths should be relative to this (eg gz/custom_msgs/vector3d.proto) MSGS_PATH ${CMAKE_CURRENT_SOURCE_DIR}/proto - # List of proto files to generate + # List of proto files to process MSGS_PROTOS ${MSGS_PROTOS} + # Depenency on gz-msgs DEPENDENCIES gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER} ) diff --git a/tutorials/message_generation.md b/tutorials/message_generation.md index ab3a420e..a7d8fc49 100644 --- a/tutorials/message_generation.md +++ b/tutorials/message_generation.md @@ -119,6 +119,10 @@ For the collection, `gz_msgs_factory` generates: Now that we understand the components of the message generation pipeline, we can use them in our own custom package. +The code for this example can be found in the `gz-msgs` [repository](https://github.com/gazebosim/gz-msgs/tree/gz-msgs10), in the [`examples/generating_custom_msgs`](https://github.com/gazebosim/gz-msgs/tree/gz-msgs10/examples/generating_custom_msgs) folder. + + + The `cmake` functionality is exported from the `gz-msgs` library, via the `gz-cmake` [`extras` functionality](https://github.com/gazebosim/gz-cmake/pull/345). To make the functions available, simply `find_package(gz-msgs10)` in your `CMakeLists.txt`: @@ -129,6 +133,7 @@ find_package(gz-cmake3 REQUIRED) find_package(gz-msgs10 REQUIRED) ``` + Next, create a directory for your custom message definitions: ```sh @@ -189,9 +194,17 @@ message BazStamped ``` -Then, back in the `CMakeLists.txt` file, generate the message library. +Then, back in the `CMakeLists.txt` file, add following lines to generate the message library: ```cmake +# Define a variable 'MSGS_PROTOS' listing the .proto files +set(MSGS_PROTOS + ${CMAKE_CURRENT_SOURCE_DIR}/proto/gz/custom_msgs/foo.proto + ${CMAKE_CURRENT_SOURCE_DIR}/proto/gz/custom_msgs/bar.proto + ${CMAKE_CURRENT_SOURCE_DIR}/proto/gz/custom_msgs/baz.proto +) + +# Call 'gz_msgs_generate_messages()' to process the .proto files gz_msgs_generate_messages( # The cmake target to be generated for libraries/executables to link TARGET msgs @@ -200,12 +213,25 @@ gz_msgs_generate_messages( # The path to the base directory of the proto files # All import paths should be relative to this (eg gz/custom_msgs/vector3d.proto) MSGS_PATH ${CMAKE_CURRENT_SOURCE_DIR}/proto - # List of proto files to generate + # List of proto files to process MSGS_PROTOS ${MSGS_PROTOS} - DEPENDENCIES gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER} + # Depenency on gz-msgs + DEPENDENCIES gz-msgs10::gz-msgs10 ) ``` +In order to reduce the amount of edits needed upon a version change of `gz-msgs`, it is common to: + + - Define a variable `GZ_MSGS_VER`, holding the version number: + ```cmake + find_package(gz-msgs10 REQUIRED) + set(GZ_MSGS_VER ${gz-msgs10_VERSION_MAJOR}) + ``` + - And change the dependency line in above code block to: + ```cmake + DEPENDENCIES gz-msgs${GZ_MSGS_VER}::gz-msgs${GZ_MSGS_VER} + ``` + ### Using Custom messages There are two primary ways that Gazebo messages are used as part of an application. @@ -270,7 +296,7 @@ $ cd build/ $ export GZ_DESCRIPTOR_PATH=`pwd` $ gz msg -l | grep "custom_msgs" | wc -l -6 +4 $ gz msg --info gz.custom_msgs.Foo Name: gz.custom_msgs.Foo