2

Hi I get an error when I build this ros2 services cpp file what is wrong? Is it about %d which is prints the value of the int variable.

EDİT: I have changed %d to %ld but then I got different error like:

> Starting >>> my_cpp_pkg
> --- stderr: my_cpp_pkg                              /usr/bin/ld: CMakeFiles/turtlebot_goal.dir/src/turtlebot_goal.cpp.o: in function
> `rclcpp::Publisher<geometry_msgs::msg::PoseStamped_<std::allocator<void>
> >, std::allocator<void> >::Publisher(rclcpp::node_interfaces::NodeBaseInterface*, std::__cxx11::basic_string<char, std::char_traits<char>,
> std::allocator<char> > const&, rclcpp::QoS const&,
> rclcpp::PublisherOptionsWithAllocator<std::allocator<void> > const&)':
> turtlebot_goal.cpp:(.text._ZN6rclcpp9PublisherIN13geometry_msgs3msg12PoseStamped_ISaIvEEES4_EC2EPNS_15node_interfaces17NodeBaseInterfaceERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_3QoSERKNS_29PublisherOptionsWithAllocatorIS4_EE[_ZN6rclcpp9PublisherIN13geometry_msgs3msg12PoseStamped_ISaIvEEES4_EC5EPNS_15node_interfaces17NodeBaseInterfaceERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_3QoSERKNS_29PublisherOptionsWithAllocatorIS4_EE]+0x6e):
> undefined reference to `rosidl_message_type_support_t const*
> rosidl_typesupport_cpp::get_message_type_support_handle<geometry_msgs::msg::PoseStamped_<std::allocator<void>
> > >()' collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/turtlebot_goal.dir/build.make:145: turtlebot_goal] Error 1
> make[1]: *** [CMakeFiles/Makefile2:389:
> CMakeFiles/turtlebot_goal.dir/all] Error 2 make: *** [Makefile:146:
> all] Error 2
> --- Failed   <<< my_cpp_pkg [0.26s, exited with code 2]

CODE:

#include "rclcpp/rclcpp.hpp"
#include "example_interfaces/srv/add_two_ints.hpp"

using std::placeholders::_1;
using std::placeholders::_2;

class AddTwoIntsServerNode : public rclcpp::Node
{
public:
    AddTwoIntsServerNode() : Node("add_two_ints_server")
    {
        server_ = this->create_service<example_interfaces::srv::AddTwoInts>(
            "add_two_ints",
            std::bind(&AddTwoIntsServerNode::callbackAddTwoInts, this, _1, _2));
        RCLCPP_INFO(this->get_logger(), "Service server has been started.");
    }

private:
    void callbackAddTwoInts(const example_interfaces::srv::AddTwoInts::Request::SharedPtr request,
                            const example_interfaces::srv::AddTwoInts::Response::SharedPtr response)
    {
        response->sum = request->a + request->b;
        RCLCPP_INFO(this->get_logger(), "%d + %d = %d", request->a, request->b, response->sum);
    }

    rclcpp::Service<example_interfaces::srv::AddTwoInts>::SharedPtr server_;
};

int main(int argc, char **argv)
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<AddTwoIntsServerNode>();
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}
gurselturkeri
  • 71
  • 1
  • 8
  • While `printf` format codes commonly aren't taught by C++ books and courses, the ROS documentation should have at least pointed out references somewhere. Or mentioned that `printf` format codes are used, to help you research it yourself. – Some programmer dude May 29 '23 at 11:04
  • 1
    The `printf` format code for `long int` is `%ld`. So one of your `%d` needs to change to `%ld`. Not really sure which one though. – john May 29 '23 at 11:06
  • This error solved when I have changed %d to %ld but I got different error you can see in question edit section. – gurselturkeri May 29 '23 at 11:21
  • Please don't change your questions. If you get new problems, please post it as new questions. – Some programmer dude May 29 '23 at 11:23

0 Answers0