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

    10.1.4章节cmakelists.txt错误

    已定时 已固定 已锁定 已移动
    动手学ROS2
    cmake报错 install文件位置
    2
    3
    310
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • 4
      开心
      最后由 编辑

      构建功能包错误日志:

      tingbo@tingbo-i7:~/chapt10/chapt10_ws$ colcon build
      Starting >>> learn_qos_cpp
      Starting >>> learn_qos_py
      --- stderr: learn_qos_cpp            
      CMake Error at /opt/ros/humble/share/ament_cmake_target_dependencies/cmake/ament_target_dependencies.cmake:42 (message):
        ament_target_dependencies() the first argument must be a valid target name
      Call Stack (most recent call first):
        CMakeLists.txt:25 (ament_target_dependencies)
      
      
      gmake: *** [Makefile:267:cmake_check_build_system] 错误 1
      ---
      Failed   <<< learn_qos_cpp [2.66s, exited with code 2]
      Aborted  <<< learn_qos_py [3.33s]                     
      
      Summary: 0 packages finished [3.99s]
        1 package failed: learn_qos_cpp
        1 package aborted: learn_qos_py
        1 package had stderr output: learn_qos_cpp
      

      cmakelists.txt代码:

      cmake_minimum_required(VERSION 3.8)
      project(learn_qos_cpp)
      
      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(nav_msgs REQUIRED)
      find_package(rclcpp 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(learn_qos_cpp src/reliability_test.cpp)
      ament_target_dependencies(reliability_test rclcpp nav_msgs)
      
      install(DIRECTORY
      reliability_test
      DESTINATION share/${PROJECT_NAME})
      
      ament_package()
      

      reliability_test代码:

      #include <nav_msgs/msg/odometry.hpp>
      #include <rclcpp/rclcpp.hpp>
      
      class OdomPublsherSubscriber : public rclcpp::Node {
          public:
              OdomPublsherSubscriber() : Node("odom_publisher_subscriber") {
                  // 创建发布者并设置 qos为sensor
                  odom_publisher_ = this->create_publisher<nav_msgs::msg::Odometry>(
                      "odom", rclcpp::SensorDataQoS());
      
                  //创建订阅者(默认Qos配置)队列设深度设置为5
                  odom_subscription_ = this->create_subscription<nav_msgs::msg::Odometry>(
                      "odom", rclcpp::SensorDataQoS(), [this] (const nav_msgs::msg::Odometry::SharedPtr msg) {
                          (void)msg;
                          RCLCPP_INFO(this->get_logger(), "收到里程计消息");
                      });
                  //创建一个1秒的定时器,并指定回调函数
                  timer_ = this->create_wall_timer(std::chrono::seconds(1), [this](){
                      odom_publisher_->publish(nav_msgs::msg::Odometry());
                  });
      
              }
          private:
              rclcpp::Publisher<nav_msgs::msg::Odometry>::SharedPtr odom_publisher_;
              rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr odom_subscription_;
              rclcpp::TimerBase::SharedPtr timer_;
      };
      
      int main(int argc, char *argv[]) {
          rclcpp::init(argc, argv);
          auto odom_node = std::make_shared<OdomPublsherSubscriber>();
          rclcpp::spin(odom_node);
          rclcpp::shutdown();
          return 0;
      }
      
      小鱼小 1 条回复 最后回复 回复 引用 0
      • 小鱼小
        小鱼 技术大佬 @43996173
        最后由 编辑

        @43996173 在你的 CMakeLists.txt 文件中,你使用了错误的目标名称。在 ament_target_dependencies() 中,第一个参数应该是你的可执行文件名,而不是源文件名。因此,你需要将 ament_target_dependencies() 中的目标名称修改为 learn_qos_cpp,而不是 reliability_test。修改后的 CMakeLists.txt 文件应该如下所示:

        cmake_minimum_required(VERSION 3.8)
        project(learn_qos_cpp)
        
        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(nav_msgs REQUIRED)
        find_package(rclcpp 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(learn_qos_cpp src/reliability_test.cpp)
        ament_target_dependencies(learn_qos_cpp rclcpp nav_msgs)  # 修改此处目标名称
        
        install(TARGETS
          learn_qos_cpp
          DESTINATION lib/${PROJECT_NAME})
        
        ament_package()
        

        请尝试这样修改,并重新构建你的软件包,看看是否解决了问题。

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

        4 1 条回复 最后回复 回复 引用 0
        • 4
          开心 @小鱼
          最后由 编辑

          @小鱼 🆗,小鱼老师

          1 条回复 最后回复 回复 引用 0
          • 第一个帖子
            最后一个帖子
          皖ICP备16016415号-7
          Powered by NodeBB | 鱼香ROS