紧急通知:禁止一切关于政治&VPN翻墙等话题,发现相关帖子会立马删除封号
小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
4.3.3 C++ 客户端代码实现rclcpp/rclcpp.hpp: 没有那个文件或目录
-
patrol_client.cpp代码(无报错)
#include <chrono> #include <cstdlib> #include <ctime> #include "rclcpp/rclcpp.hpp" #include "chapt4_interfaces/srv/patrol.hpp" using namespace std::chrono_literals; using Patrol = chapt4_interfaces::srv::Patrol; class PatrolClient : public rclcpp::Node { public: PatrolClient() : Node("patrol_client") { patrol_client_ = this->create_client<Patrol>("patrol"); timer_ = this->create_wall_timer(10s, std::bind(&PatrolClient::timer_callback, this)); srand(time(NULL)); } void timer_callback() { while (!patrol_client_->wait_for_service(std::chrono::seconds(1))) { if(!rclcpp::ok()) { RCLCPP_ERROR(this->get_logger(),"等待服务端过程中被打断..."); return; } RCLCPP_INFO(this->get_logger(),"等待服务端上线中"); } auto request = std::make_shared<Patrol::Request>(); request->target_x = rand() % 15; request->target_y = rand() % 15; RCLCPP_INFO(this->get_logger(), "请求巡逻:(%f,%f)",request->target_x, request->target_y); patrol_client_->async_send_request( request, [&](rclcpp::Client<Patrol>::SharedFuture result_future) -> void { auto response = result_future.get(); if (response->result == Patrol::Response::SUCCESS) { RCLCPP_INFO(this->get_logger(), "目标点处理成功"); } else if (response->result == Patrol::Response::FALL) { RCLCPP_INFO(this->get_logger(), "目标点处理失败"); } }); } private: rclcpp::TimerBase::SharedPtr timer_; rclcpp::Client<Patrol>::SharedPtr patrol_client_; }; int main(int argc, char **argv) { rclcpp::init(argc, argv); auto node = std::make_shared<PatrolClient>(); rclcpp::spin(node); rclcpp::shutdown(); return 0; }
Cmakelists.txt文件:
cmake_minimum_required(VERSION 3.8) project(demo_cpp_service) 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(chapt4_interfaces REQUIRED) find_package(rclcpp REQUIRED) find_package(geometry_msgs REQUIRED) find_package(turtlesim 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(turtle_control src/turtle_control.cpp) add_executable(patrol_client src/patrol_client.cpp) ament_target_dependencies(turtle_control rclcpp geometry_msgs turtlesim chapt4_interfaces) install( TARGETS turtle_control patrol_client DESTINATION lib/${PROJECT_NAME} ) ament_package()
colcon build报错:
tarting >>> chapt4_interfaces Finished <<< chapt4_interfaces [0.42s] Starting >>> demo_cpp_service Starting >>> demo_python_service Finished <<< demo_python_service [0.82s] --- stderr: demo_cpp_service /home/tingbo/chapt4/chapt4_ws/src/demo_cpp_service/src/patrol_client.cpp:4:10: fatal error: rclcpp/rclcpp.hpp: 没有那个文件或目录 4 | #include "rclcpp/rclcpp.hpp" | ^~~~~~~~~~~~~~~~~~~ compilation terminated. gmake[2]: *** [CMakeFiles/patrol_client.dir/build.make:76:CMakeFiles/patrol_client.dir/src/patrol_client.cpp.o] 错误 1 gmake[1]: *** [CMakeFiles/Makefile2:165:CMakeFiles/patrol_client.dir/all] 错误 2 gmake: *** [Makefile:146:all] 错误 2 --- Failed <<< demo_cpp_service [5.95s, exited with code 2] Summary: 2 packages finished [6.59s] 1 package failed: demo_cpp_service 1 package had stderr output: demo_cpp_service
目录配置文件
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/opt/ros/${ROS_DISTRO}/include/**", "/opt/ros/humble/include" ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c17", "cppStandard": "gnu++17", "intelliSenseMode": "linux-gcc-x64" } ], "version": 4 }
-
@43996173 如何你在代码中引用了某个库,那么你应该像给turtle_control链接库一样,给patrol_client练级相应的依赖库。
add_executable(turtle_control src/turtle_control.cpp) add_executable(patrol_client src/patrol_client.cpp) ament_target_dependencies(turtle_control rclcpp geometry_msgs turtlesim chapt4_interfaces) # 添加该条命令 ament_target_dependencies(patrol_client rclcpp geometry_msgs turtlesim chapt4_interfaces)
以后请按照模板提问: https://fishros.org.cn/forum/topic/151/提问前必看-不符合要求的问题拒绝回答
-
@小鱼 ok,谢谢老师!