Compilation warnings for subscribers and publishers deprecated syntax

When I follow the course syntax, I encounter some compilation warnings about deprecated syntax as follows: (NOTE: these are warnings, the examples still “work” functionally).

Publisher warning:

Starting >>> topic_subscriber_pkg
--- stderr: topic_publisher_pkg
/home/user/ros2_ws/src/topic_publisher_pkg/src/cmd_vel_publisher.cpp: In constructor 'SimplePublisher::SimplePublisher()':
/home/user/ros2_ws/src/topic_publisher_pkg/src/cmd_vel_publisher.cpp:13:77: warning: 'std::shared_ptr<PublisherT> rclcpp::Node::create_publisher
(const string&, const rmw_qos_profile_t&, std::shared_ptr<_Up>) [with MessageT = geometry_msgs::msg::Twist_<std::allocator<void> >; AllocatorT =
 std::allocator<void>; PublisherT = rclcpp::Publisher<geometry_msgs::msg::Twist_<std::allocator<void> > >; std::__cxx11::string = std::__cxx11::
basic_string<char>; rmw_qos_profile_t = rmw_qos_profile_t]' is deprecated: use create_publisher(const std::string &, const rclcpp::QoS &, ...) i
nstead [-Wdeprecated-declarations]
     publisher_ = this->create_publisher<geometry_msgs::msg::Twist>("cmd_vel");
                                                                             ^
In file included from /opt/ros/dashing/include/rclcpp/node.hpp:1206:0,
                 from /opt/ros/dashing/include/rclcpp/executors/single_threaded_executor.hpp:28,
                 from /opt/ros/dashing/include/rclcpp/executors.hpp:22,
                 from /opt/ros/dashing/include/rclcpp/rclcpp.hpp:144,
                 from /home/user/ros2_ws/src/topic_publisher_pkg/src/cmd_vel_publisher.cpp:2:
/opt/ros/dashing/include/rclcpp/node_impl.hpp:96:1: note: declared here
 Node::create_publisher(
 ^~~~
/home/user/ros2_ws/src/topic_publisher_pkg/src/cmd_vel_publisher.cpp: In member function 'void SimplePublisher::timer_callback()':
/home/user/ros2_ws/src/topic_publisher_pkg/src/cmd_vel_publisher.cpp:23:32: warning: 'void rclcpp::Publisher<MessageT, Alloc>::publish(const std::shared_ptr<const _Tp>&) [with MessageT = geometry_msgs::msg::Twist_<std::allocator<void> >; Alloc = std::allocator<void>]' is deprecated: publishing an unique_ptr is prefered when using intra process communication. If using a shared_ptr, use publish(*msg). [-Wdeprecated-declarations]
     publisher_->publish(message);
                                ^
In file included from /opt/ros/dashing/include/rclcpp/callback_group.hpp:24:0,
                 from /opt/ros/dashing/include/rclcpp/any_executable.hpp:20,
                 from /opt/ros/dashing/include/rclcpp/memory_strategy.hpp:24,
                 from /opt/ros/dashing/include/rclcpp/memory_strategies.hpp:18,
                 from /opt/ros/dashing/include/rclcpp/executor.hpp:33,
                 from /opt/ros/dashing/include/rclcpp/executors/multi_threaded_executor.hpp:24,
                 from /opt/ros/dashing/include/rclcpp/executors.hpp:21,
                 from /opt/ros/dashing/include/rclcpp/rclcpp.hpp:144,
                 from /home/user/ros2_ws/src/topic_publisher_pkg/src/cmd_vel_publisher.cpp:2:
/opt/ros/dashing/include/rclcpp/publisher.hpp:137:3: note: declared here
   publish(const std::shared_ptr<const MessageT> & msg)
   ^~~~~~~

Subscriber warning:

--- stderr: topic_subscriber_pkg
/home/user/ros2_ws/src/topic_subscriber_pkg/src/simple_topic_subscriber.cpp: In function 'int main(int, char**)':
/home/user/ros2_ws/src/topic_subscriber_pkg/src/simple_topic_subscriber.cpp:14:32: warning: 'std::shared_ptr<SubscriptionT> rclcpp::Node::create_subscription(const string&, CallbackT&&, const rmw_qos_profile_t&, rclcpp::callback_group::CallbackGroup::SharedPtr, bool, typename rclcpp::message_memory_strategy::MessageMemoryStrategy<typename rclcpp::subscription_traits::has_message_type<CallbackT>::type, AllocatorT>::SharedPtr, std::shared_ptr<PublisherT>) [with MessageT = std_msgs::msg::Int32_<std::allocator<void> >; CallbackT = void (&)(std::shared_ptr<std_msgs::msg::Int32_<std::allocator<void> > >); Alloc = std::allocator<void>; SubscriptionT = rclcpp::Subscription<std_msgs::msg::Int32_<std::allocator<void> >,std::allocator<void> >; std::__cxx11::string = std::__cxx11::basic_string<char>; rmw_qos_profile_t = rmw_qos_profile_t; rclcpp::callback_group::CallbackGroup::SharedPtr = std::shared_ptr<rclcpp::callback_group::CallbackGroup>; typename rclcpp::message_memory_strategy::MessageMemoryStrategy<typename rclcpp::subscription_traits::has_message_type<CallbackT>::type, AllocatorT>::SharedPtr = std::shared_ptr<rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::Int32_<std::allocator<void> >, std::allocator<void> > >]' is deprecated: use create_subscription(const std::string &, const rclcpp::QoS &, CallbackT, ...) instead [-Wdeprecated-declarations]
       "counter", topic_callback);
                                ^
In file included from /opt/ros/dashing/include/rclcpp/node.hpp:1206:0,
                 from /opt/ros/dashing/include/rclcpp/executors/single_threaded_executor.hpp:28,
                 from /opt/ros/dashing/include/rclcpp/executors.hpp:22,
                 from /opt/ros/dashing/include/rclcpp/rclcpp.hpp:144,
                 from /home/user/ros2_ws/src/topic_subscriber_pkg/src/simple_topic_subscriber.cpp:1:
/opt/ros/dashing/include/rclcpp/node_impl.hpp:139:1: note: declared here
 Node::create_subscription(
 ^~~~

Is there a more updated syntax we should be using?

Hi @swarooph.nirmal,

Thank you for letting us know. Let’s look into it and get back to you.

In the meantime, please move ahead with your learning since it’s not stopping you :+1:.

Hello @swarooph.nirmal,

In order to get rid of tis warnings, in the publisher, you should use the following syntax:

#include "rclcpp/rclcpp.hpp"

#include "std_msgs/msg/int32.hpp"

int main(int argc, char * argv[])

{

  rclcpp::init(argc, argv);

  auto node = rclcpp::Node::make_shared("simple_publisher");

  auto publisher = node->create_publisher<std_msgs::msg::Int32>("counter", 10);

  auto message = std::make_shared<std_msgs::msg::Int32>();

  message->data = 0;

  rclcpp::WallRate loop_rate(2);

  while (rclcpp::ok()) {

    

    publisher->publish(*message);

    message->data++;

    rclcpp::spin_some(node);

    loop_rate.sleep();

  }

  rclcpp::shutdown();

  return 0;

}

Note the only difference is that we are explicitly specifying the size of the queue (10) in the publisher creation. Also, when accessing the *message for publishing, we do it as a shared pointer.

For the subscriber, you would need to specify the queue size as well, like this:

auto subscription = g_node->create_subscription<std_msgs::msg::Int32>(
      "counter", 10, topic_callback);

I have already updated this in the notebook as well.

Hope this helps,

1 Like

Thank you! Specifying the queue size seems to have solved my issue.

1 Like