紧急通知:禁止一切关于政治&VPN翻墙等话题,发现相关帖子会立马删除封号
小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
10.1.4章节cmakelists.txt错误
-
构建功能包错误日志:
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; }
-
@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()
请尝试这样修改,并重新构建你的软件包,看看是否解决了问题。
-
@小鱼 ,小鱼老师