紧急通知:禁止一切关于政治&VPN翻墙等话题,发现相关帖子会立马删除封号
小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
makefile编译ros_humble
-
版本: ros_humble
cpp程序#include "rclcpp/rclcpp.hpp" #include "std_msgs/msg/string.hpp" class TopicPublisher01 : public rclcpp::Node { public: // 构造函数,有一个参数为节点名称 TopicPublisher01(std::string name) : Node(name) { RCLCPP_INFO(this->get_logger(), "大家好,我是%s.", name.c_str()); // 创建发布者 command_publisher_ = this->create_publisher<std_msgs::msg::String>("command", 10); // 创建定时器,500ms为周期,定时发布 timer_ = this->create_wall_timer(std::chrono::milliseconds(500), std::bind(&TopicPublisher01::timer_callback, this)); } private: void timer_callback() { // 创建消息 std_msgs::msg::String message; message.data = "forward"; // 日志打印 RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str()); // 发布消息 command_publisher_->publish(message); } // 声名定时器指针 rclcpp::TimerBase::SharedPtr timer_; // 声明话题发布者指针 rclcpp::Publisher<std_msgs::msg::String>::SharedPtr command_publisher_; }; int main(int argc, char **argv) { rclcpp::init(argc, argv); /*产生一个的节点*/ auto node = std::make_shared<TopicPublisher01>("first_node"); /* 运行节点,并检测退出信号*/ rclcpp::spin(node); rclcpp::shutdown(); return 0; }
makefile程序
build: g++ ros2_makefile.cpp \ -I /opt/ros/humble/include/rclcpp/ \ -I /opt/ros/humble/include/rcl/ \ -I /opt/ros/humble/include/rcutils/ \ -I /opt/ros/humble/include/rmw \ -I /opt/ros/humble/include/rcl_yaml_param_parser/ \ -I /opt/ros/humble/include/rosidl_runtime_c \ -I /opt/ros/humble/include/rosidl_typesupport_interface \ -I /opt/ros/humble/include/rcpputils \ -I /opt/ros/humble/include/builtin_interfaces \ -I /opt/ros/humble/include/rosidl_runtime_cpp \ -I /opt/ros/humble/include/tracetools \ -I /opt/ros/humble/include/rcl_interfaces \ -I /opt/ros/humble/include/libstatistics_collector \ -I /opt/ros/humble/include/statistics_msgs \ -I /opt/ros/humble/include/std_msgs \ -L /opt/ros/humble/lib/ \ -lrclcpp -lrcutils -lrcl -ltracetools -lrmw \ -o first_node # 顺便小鱼加个clean指令,用来删掉first_node clean: rm first_node
报错
~/Desktop/IGH_Master/ROS2_Makefile$ make build g++ ros2_makefile.cpp \ -I /opt/ros/humble/include/rclcpp/ \ -I /opt/ros/humble/include/rcl/ \ -I /opt/ros/humble/include/rcutils/ \ -I /opt/ros/humble/include/rmw \ -I /opt/ros/humble/include/rcl_yaml_param_parser/ \ -I /opt/ros/humble/include/rosidl_runtime_c \ -I /opt/ros/humble/include/rosidl_typesupport_interface \ -I /opt/ros/humble/include/rcpputils \ -I /opt/ros/humble/include/builtin_interfaces \ -I /opt/ros/humble/include/rosidl_runtime_cpp \ -I /opt/ros/humble/include/tracetools \ -I /opt/ros/humble/include/rcl_interfaces \ -I /opt/ros/humble/include/libstatistics_collector \ -I /opt/ros/humble/include/statistics_msgs \ -I /opt/ros/humble/include/std_msgs \ -L /opt/ros/humble/lib/ \ -lrclcpp -lrcutils -lrcl -ltracetools -lrmw \ -o first_node /usr/bin/ld: /tmp/ccH6HcBC.o: in function `std::enable_if<rosidl_generator_traits::is_message<std_msgs::msg::String_<std::allocator<void> > >::value, rosidl_message_type_support_t const&>::type rclcpp::get_message_type_support_handle<std_msgs::msg::String_<std::allocator<void> > >()': ros2_makefile.cpp:(.text._ZN6rclcpp31get_message_type_support_handleIN8std_msgs3msg7String_ISaIvEEEEENSt9enable_ifIXsrN23rosidl_generator_traits10is_messageIT_EE5valueERK29rosidl_message_type_support_tE4typeEv[_ZN6rclcpp31get_message_type_support_handleIN8std_msgs3msg7String_ISaIvEEEEENSt9enable_ifIXsrN23rosidl_generator_traits10is_messageIT_EE5valueERK29rosidl_message_type_support_tE4typeEv]+0x10): 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: *** [makefile:2:build] 错误 1
-
-
@847718956 因为你引入了std_msgs这个包了,所以指令里也要包含这个,不过用gcc非常的不方便,还是推荐你用ROS2的工程或者CmakeLists.txt文件来生成可执行文件
@847718956 在 makefile编译ros_humble 中说:
#include "std_msgs/msg/string.hpp"