鱼香ROS社区
    • 版块
    • 最新
    • 未解决
    • 已解决
    • 群组
    • 注册
    • 登录
    紧急通知:禁止一切关于政治&VPN翻墙等话题,发现相关帖子会立马删除封号
    提问前必看的发帖注意事项: 社区问答规则(小鱼个人)更新 | 高质量帖子发布指南

    ros2用colcon编译时报错文件依赖找不到

    已定时 已固定 已锁定 已移动 已解决
    ROS 2相关问题
    ros2编译报错 缺少依赖 ros2编译
    2
    6
    780
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • 3307093903
      小豆
      最后由 编辑

      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都改了,文件目录也没错,但节点文件依旧标红,编译时也确实报了错
      替代文字

      小鱼小 1 条回复 最后回复 回复 引用 0
      • 3307093903
        小豆 @小鱼
        最后由 编辑

        @小鱼 抱歉,我这一节都是直接复制黏贴,没注意看源码,其实原文"2.话题之RCLCPP实现"那里用API设计发布者时展示的源码只是面向对象设计的类TopicPublisher01,没有main函数,导致编译时报错了

        1 条回复 最后回复 回复 引用 0
        • 小鱼小
          小鱼 技术大佬 @330709390
          最后由 编辑

          @330709390 把设计到的文件代码都贴一下

          @小鱼 在 提问前必看!一定要看!必须看一下! 中说:

          问题一定要描述清楚,终端打印一定复制粘贴,方便回答者检索和引用(你可以在linux系统上打开浏览器进社区)
          基本的Markdown语法一定要学习下,有的小伙伴图片代码一团糟
          提问时一定要提供尽可能多的信息(系统版本,ROS版本,前后操作,终端日志),包括你的目的,比如你其实想装装某个库遇到问题,不要只说这个问题,因为可能有更好的替代方案
          先搜索再提问,很多问题其实都有解决方案,确保你自己对自己的问题有一定了解再提问
          尽量一句话说完,不要把社区当微信聊天一样用,每一个回复都尽量提供更多的的信息。

          新书配套视频:https://www.bilibili.com/video/BV1GW42197Ck/

          3307093903 1 条回复 最后回复 回复 引用 0
          • 3307093903
            小豆 @小鱼
            最后由 编辑

            @小鱼
            这是cmakelist

            cmake_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

            3307093903 小鱼小 2 条回复 最后回复 回复 引用 0
            • 3307093903
              小豆 @330709390
              最后由 编辑

              @330709390 调换过ament_package()的位置依旧报一样的错

              1 条回复 最后回复 回复 引用 0
              • 小鱼小
                小鱼 技术大佬 @330709390
                最后由 编辑

                @330709390 在 ros2用colcon编译时报错文件依赖找不到 中说:

                01,cpp

                01.cpp 吧

                另外你的cpp文件里没有main函数

                新书配套视频:https://www.bilibili.com/video/BV1GW42197Ck/

                3307093903 1 条回复 最后回复 回复 引用 0
                • 3307093903
                  小豆 @小鱼
                  最后由 编辑

                  @小鱼 抱歉,我这一节都是直接复制黏贴,没注意看源码,其实原文"2.话题之RCLCPP实现"那里用API设计发布者时展示的源码只是面向对象设计的类TopicPublisher01,没有main函数,导致编译时报错了

                  1 条回复 最后回复 回复 引用 0
                  • 3307093903 330709390 将这个主题标记为已解决,在
                  • 第一个帖子
                    最后一个帖子
                  皖ICP备16016415号-7
                  Powered by NodeBB | 鱼香ROS