首页 > 解决方案 > 致命错误:ros/ros.h:没有这样的文件或目录 1 | #include "ros/ros.h"

问题描述

有很多与此相关的问题,但我找不到任何对我有用的东西。我创建了一个 C++ 脚本来订阅相机主题。当我尝试运行脚本时,出现以下错误。

receiver.cpp:1:10: fatal error: ros/ros.h: No such file or directory
    1 | #include "ros/ros.h"
      |          ^~~~~~~~~~~

根据我在互联网上的搜索,错误是由于 CMake 文件造成的。我尝试了各种方法无法弄清楚为什么。所以我还在下面添加了我的 CMake 列表文件。请调查一下。

cmake_minimum_required(VERSION 3.0.2)
project(cv_basics)

## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)

include_directories(${OpenCV_INCLUDE_DIRS})
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  roscpp
  rospy
  sensor_msgs
  std_msgs
  nav_msgs
)

## Generate messages in the 'msg' folder
# add_message_files(
#   FILES
#   Message1.msg
#   Message2.msg
# )

## Generate services in the 'srv' folder
# add_service_files(
#   FILES
#   Service1.srv
#   Service2.srv
# )

## Generate actions in the 'action' folder
# add_action_files(
#   FILES
#   Action1.action
#   Action2.action
# )

## Generate added messages and services with any dependencies listed here
#generate_messages(
 #  DEPENDENCIES
  # sensor_msgs#   std_msgs
# )



catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES cv_basics
#  CATKIN_DEPENDS cv_bridge image_transport roscpp rospy sensor_msgs std_msgs
#  DEPENDS system_lib
)

include_directories(
 include
  ${catkin_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
)

我正在使用 ros noetic 和 ubuntu 20

标签: c++cmakeros

解决方案


您需要确保使用CMakeLists.txt文件中的 ros 库进行构建。确保你有这些行:

add_executable(some_exe src/your_source.cpp)
target_link_libraries(some_exe ${catkin_LIBRARIES})

当然,在你的包中用它们各自正确的名称替换some_exe和。src/your_source.cpp


推荐阅读