Robotics StackExchange | Archived questions

Error compiling c++ code (ros/ros.h: No such file or directory)

Hello. Im using ubuntu 20.4 , ros noetic and gazebo 11 trying to move a robot using cubic polynomials and c++. At the beginning of the file I have these inlcudes:

#include <ros/ros.h>
#include <tf/tf.h>
#include <geometry_msgs/Twist.h>
#include <geometry_msgs/Pose.h>
#include <nav_msgs/Odometry.h>
#include <math.h>

When I compile the file ,I get this error:

traj_gen_cubic.cpp:1:10: fatal error: ros/ros.h: No such file or directory
    1 | #include <ros/ros.h>
      |          ^~~~~~~~~~~
compilation terminated.

This is my CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.3)
project(husky_ur3_gazebo)

find_package(catkin REQUIRED COMPONENTS
  roscpp
  std_msgs
  roslaunch
)

catkin_package(
  CATKIN_DEPENDS roscpp std_msgs
)
include_directories(${catkin_INCLUDE_DIRS})


add_executable(gazebo_rh_pub src/gazebo_rh_pub.cpp)
add_dependencies(gazebo_rh_pub ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(gazebo_rh_pub ${catkin_LIBRARIES})

install(TARGETS gazebo_rh_pub
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)


install(DIRECTORY launch controller DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION})

What should I do?

Asked by NickRos on 2023-04-04 11:17:57 UTC

Comments

what's your compile command?are you sure you have source(init) the ros env? as http://wiki.ros.org/ROS/Tutorials/BuildingPackages

Asked by fury.nerd on 2023-04-06 04:16:22 UTC

g++ traj_gen_cubic.cpp -o traj_gen_cubic

Asked by NickRos on 2023-04-06 09:45:35 UTC

This command is also inside my .bashrc file so I dont have to write it every time $ source /opt/ros/noetic/setup.bash

Asked by NickRos on 2023-04-06 09:47:48 UTC

When i add this line "add_executable(traj_gen_cubic src/traj_gen_cubic.cpp) "in CMakeLists.txt file and I do catkin_make ,I get this error: Invoking "make -j4 -l4" failed

Asked by NickRos on 2023-04-09 12:57:52 UTC

Any ideas??

Asked by NickRos on 2023-04-12 09:20:57 UTC

Answers

This error indicates that your compiler cannot find ros/ros.h. You probably need to source your ros setup.bash in the newly opened terminal before building. If you are following the tutorial to install noetic, try running:

$ source /opt/ros/noetic/setup.bash

before building.

Asked by GuimingChen on 2023-04-06 08:47:17 UTC

Comments

Same error

Asked by NickRos on 2023-04-06 09:42:49 UTC

This command is also inside my .bashrc file so I dont have to write it every time

Asked by NickRos on 2023-04-06 09:47:38 UTC

g++ traj_gen_cubic.cpp -o traj_gen_cubic

This is not how you build a ros node. You are expected to use either catkin_make or catkin build. For our projects, we use catkin_make.

Update: my CMakeLists.txt file for noetic has differences from yours:

  1. Prior to the find_package() line I have:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

  2. My catkin_package() looks like:

    catkin_package( CATKIN_DEPENDS roscpp std_msgs INCLUDE_DIRS include )

Asked by Mike Scheutzow on 2023-04-07 06:21:52 UTC

Comments

Yes of course I have built my node using catkin_make but I think I miss something

Asked by NickRos on 2023-04-07 07:43:20 UTC

traj_gen_cubic.cpp does not appear in CMakeLists.txt.

It may be necessary to add the following, for example. This is incorrect depending on the file division of your project.

add_executable(traj_gen_cubic src/traj_gen_cubic.cpp)

Asked by miura on 2023-04-08 07:42:56 UTC

Comments

Hah! Nice catch. Sometimes it's the simple stuff :-)

Asked by Mike Scheutzow on 2023-04-08 08:49:27 UTC

When i add this line in CMakeLists.txt file and I do catkin_make ,I get this error: Invoking "make -j4 -l4" failed

Asked by NickRos on 2023-04-09 06:36:27 UTC

Did you add the corresponding add_dependencies and target_link_libraries lines as well?

Asked by pcoenen on 2023-04-13 07:07:32 UTC