ros2用colcon编译时报错文件依赖找不到
-
Starting >>> example_topic_rclcpp --- stderr: example_topic_rclcpp /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in function `_start': (.text+0x1b): undefined reference to `main' collect2: error: ld returned 1 exit status gmake[2]: *** [CMakeFiles/topic_publisher_01.dir/build.make:159: topic_publisher_01] Error 1 gmake[1]: *** [CMakeFiles/Makefile2:137: CMakeFiles/topic_publisher_01.dir/all] Error 2 gmake: *** [Makefile:146: all] Error 2 --- Failed <<< example_topic_rclcpp [0.74s, exited with code 2] Summary: 0 packages finished [0.88s] 1 package failed: example_topic_rclcpp 1 package had stderr output: example_topic_rclcpp
求问各位这里怎么出错的,一步一步跟着小鱼的教程来的,功能包也编译进去了,cmakelist和package都改了,文件目录也没错,但节点文件依旧标红,编译时也确实报了错
-
@小鱼 抱歉,我这一节都是直接复制黏贴,没注意看源码,其实原文"2.话题之RCLCPP实现"那里用API设计发布者时展示的源码只是面向对象设计的类TopicPublisher01,没有main函数,导致编译时报错了
-
@330709390 把设计到的文件代码都贴一下
@小鱼 在 提问前必看!一定要看!必须看一下! 中说:
问题一定要描述清楚,终端打印一定复制粘贴,方便回答者检索和引用(你可以在linux系统上打开浏览器进社区)
基本的Markdown语法一定要学习下,有的小伙伴图片代码一团糟
提问时一定要提供尽可能多的信息(系统版本,ROS版本,前后操作,终端日志),包括你的目的,比如你其实想装装某个库遇到问题,不要只说这个问题,因为可能有更好的替代方案
先搜索再提问,很多问题其实都有解决方案,确保你自己对自己的问题有一定了解再提问
尽量一句话说完,不要把社区当微信聊天一样用,每一个回复都尽量提供更多的的信息。 -
@小鱼
这是cmakelistcmake_minimum_required(VERSION 3.8) project(example_topic_rclcpp) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() # find dependencies find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) # the following line skips the linter which checks for copyrights # comment the line when a copyright and license is added to all source files set(ament_cmake_copyright_FOUND TRUE) # the following line skips cpplint (only works in a git repo) # comment the line when this package is in a git repo and when # a copyright and license is added to all source files set(ament_cmake_cpplint_FOUND TRUE) ament_lint_auto_find_test_dependencies() endif() add_executable(topic_publisher_01 src/topic_publisher_01.cpp) ament_target_dependencies(topic_publisher_01 rclcpp std_msgs) ament_package()
这是package.xml
<?xml version="1.0"?> <?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> <package format="3"> <name>example_topic_rclcpp</name> <version>0.0.0</version> <description>TODO: Package description</description> <maintainer email="bigbean@todo.todo">bigbean</maintainer> <license>TODO: License declaration</license> <buildtool_depend>ament_cmake</buildtool_depend> <depend>rclcpp</depend> <depend>std_msgs</depend> <test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_common</test_depend> <export> <build_type>ament_cmake</build_type> </export> </package>
这是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_; };
这是文件夹顺序
chapt3_ws
--|src
--|--|example_topic_rclcpp
--|--|--|src
--|--|--|--|topic_publisher_01,cpp
--|--|--|CMakeLists.txt
--|--|--|package.xml
--|--|--|include
--|--|--|--|example_topic_rclcpp -
@330709390 调换过ament_package()的位置依旧报一样的错
-
-
@小鱼 抱歉,我这一节都是直接复制黏贴,没注意看源码,其实原文"2.话题之RCLCPP实现"那里用API设计发布者时展示的源码只是面向对象设计的类TopicPublisher01,没有main函数,导致编译时报错了
-