// Create a Listener class that subclasses the generic rclcpp::Node base class. // The main function below will instantiate the class as a ROS node. classListener :public rclcpp::Node { public: Listener() : Node("listener") { sub_ = this->create_subscription<std_msgs::msg::String>( "chatter", std::bind(&Listener::callback, this, std::placeholders::_1)); }
private: voidcallback(const std_msgs::msg::String::SharedPtr msg){ RCLCPP_INFO(this->get_logger(), "I heard: [%s]", msg->data.c_str()); }
intmain(int argc, char *argv[]) { rclcpp::init(argc, argv); auto node = std::make_shared<Listener>(); auto node2 = rclcpp::Node::make_shared("talker"); rclcpp::spin(node); rclcpp::shutdown(); return0; }
rclcpp::Node::create_subscription requires callback functions to be type of function object. function object (or functor) is a class that defines operator() callback() is non-static member function, so it’s required to pass this pointer.
$ objdump install/ros_course_demo/lib/ros_course_demo/talker -t install/ros_course_demo/lib/ros_course_demo/talker: file format elf64-x86-64
SYMBOL TABLE: ... 0000000000015d40 w F .text 000000000000004d _ZN6TalkerD1Ev 0000000000011806 w F .text 000000000000015b _ZNSt23_Sp_counted_ptr_inplaceIN6rclcpp9PublisherIN8 std_msgs3msg7String_ISaIvEEES5_EESaIS7_ELN9__gnu_cxx12_Lock_policyE2EEC2IJRPNS0_15node_interfaces17NodeBaseInterfa ceERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEER23rcl_publisher_options_tRKNS0_23PublisherEventCallbacks ERSt10shared_ptrISaIS6_EEEEES8_DpOT_ ...
C++ uses name mangling to handle classes, templates, namespaces, etc. Use c++filt to demangle symbols:
$ objdump install/ros_course_demo/lib/ros_course_demo/talker -t \ | c++filt | grep callback ... 0000000000008944 w F .text 000000000000031c Talker::callback() ...
Retrieve the symbol in ELF file for Talker::callback() using address 0000000000008944.
$ objdump install/ros_course_demo/lib/ros_course_demo/talker -t \ | grep 0000000000008944 0000000000008944 w F .text 000000000000031c _ZN6Talker8callbackEv
其實要找到 demangled symbol 只需要使用 $ nm -C 就可以了,感覺根本不需要這麼複雜。