I am trying simple ROS2 publisher example but I am getting following error.

asked 2019-06-11 04:13:55 -0500

prajyot gravatar image

updated 2019-06-12 00:28:04 -0500

jayess gravatar image

Code:

#include <chrono>
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;

class MinimalPublisher: public rclcpp::Node {

  public: MinimalPublisher(): Node("minimal_publisher"),
  count_(0) {
    publisher_ = this - > create_publisher < std_msgs::msg::String > ("chatter", 10);
    timer_ = this - > create_wall_timer(500 ms, std::bind( & MinimalPublisher::timer_callback, this));
  }

  private: void timer_callback() {
    auto message = std_msgs::msg::String();
    message.data = "Hello, world! " + std::to_string(count_++);
    RCLCPP_INFO(this - > get_logger(), "Publishing: '%s'", message.data.c_str());
    publisher_ - > publish(message);
  }
  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::Publisher < std_msgs::msg::String > ::SharedPtr publisher_;
  size_t count_;
};

Straight dump of the error message:

--- stderr: my_pkg                              
CMakeFiles/my_pkg.dir/ariac_node.cpp.o: In function `rclcpp::Publisher<std_msgs::msg::String_<std::allocator<void> >, std::allocator<void> >::Publisher(rclcpp::node_interfaces::NodeBaseInterface*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, rcl_publisher_options_t const&, std::shared_ptr<std::allocator<std_msgs::msg::String_<std::allocator<void> > > > const&)':
ariac_node.cpp:(.text._ZN6rclcpp9PublisherIN8std_msgs3msg7String_ISaIvEEES4_EC2EPNS_15node_interfaces17NodeBaseInterfaceERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERK23rcl_publisher_options_tRKSt10shared_ptrISaIS5_EE[_ZN6rclcpp9PublisherIN8std_msgs3msg7String_ISaIvEEES4_EC5EPNS_15node_interfaces17NodeBaseInterfaceERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERK23rcl_publisher_options_tRKSt10shared_ptrISaIS5_EE]+0x22): undefined reference to `rosidl_message_type_support_t const* rosidl_typesupport_cpp::get_message_type_support_handle<std_msgs::msg::String_<std::allocator<void> > >()'
collect2: error: ld returned 1 exit status
make[2]: *** [my_pkg] Error 1
make[1]: *** [CMakeFiles/my_pkg.dir/all] Error 2
make: *** [all] Error 2
---
Failed   <<< my_pkg [ Exited with code 2 ]

Summary: 0 packages finished [6.04s]
  1 package failed: my_pkg
  1 package had stderr output: my_pkg
edit retag flag offensive close merge delete

Comments

I am trying simple ROS2 publisher example

What example?

jayess gravatar imagejayess ( 2019-06-11 17:34:32 -0500 )edit

include <chrono>

    #include <memory>

    #include "rclcpp/rclcpp.hpp"
    #include "std_msgs/msg/string.hpp"

    using namespace std::chrono_literals;


    class MinimalPublisher : public rclcpp::Node
    {

    public:MinimalPublisher(): Node("minimal_publisher"), count_(0)
    {
    publisher_ = this->create_publisher<std_msgs::msg::String>("chatter", 10);
    timer_ = this->create_wall_timer(500ms, std::bind(&MinimalPublisher::timer_callback, this));
    }

    private:
    void timer_callback()
    {
    auto message = std_msgs::msg::String();
    message.data = "Hello, world! " + std::to_string(count_++);
    RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
    publisher_->publish(message);
    }
    rclcpp::TimerBase::SharedPtr timer_;
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
    size_t count_;
    };
prajyot gravatar imageprajyot ( 2019-06-12 00:07:35 -0500 )edit

Please don't use comments like that, that's not what they're there for. I've updated your question with the code.

jayess gravatar imagejayess ( 2019-06-12 00:28:33 -0500 )edit

Thanks for suggestion..

prajyot gravatar imageprajyot ( 2019-06-12 01:44:44 -0500 )edit

This is a linker error which implies your projects dependencies have not been setup correctly. Is there a link to the tutorial you're using.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2019-06-12 09:51:53 -0500 )edit