ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

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

asked 2023-04-04 11:17:57 -0600

NickRos gravatar image

updated 2023-04-04 11:22:53 -0600

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?

edit retag flag offensive close merge delete

Comments

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

fury.nerd gravatar image fury.nerd  ( 2023-04-06 04:16:22 -0600 )edit

g++ traj_gen_cubic.cpp -o traj_gen_cubic

NickRos gravatar image NickRos  ( 2023-04-06 09:45:35 -0600 )edit

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

NickRos gravatar image NickRos  ( 2023-04-06 09:47:48 -0600 )edit

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

NickRos gravatar image NickRos  ( 2023-04-09 12:57:52 -0600 )edit

Any ideas??

NickRos gravatar image NickRos  ( 2023-04-12 09:20:57 -0600 )edit

3 Answers

Sort by ยป oldest newest most voted
1

answered 2023-04-07 06:21:52 -0600

Mike Scheutzow gravatar image

updated 2023-04-08 07:44:40 -0600

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 )

edit flag offensive delete link more

Comments

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

NickRos gravatar image NickRos  ( 2023-04-07 07:43:20 -0600 )edit
0

answered 2023-04-08 07:42:56 -0600

miura gravatar image

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)
edit flag offensive delete link more

Comments

1

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

Mike Scheutzow gravatar image Mike Scheutzow  ( 2023-04-08 08:49:27 -0600 )edit

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

NickRos gravatar image NickRos  ( 2023-04-09 06:36:27 -0600 )edit
1

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

pcoenen gravatar image pcoenen  ( 2023-04-13 07:07:32 -0600 )edit
0

answered 2023-04-06 08:47:17 -0600

GuimingChen gravatar image

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.

edit flag offensive delete link more

Comments

Same error

NickRos gravatar image NickRos  ( 2023-04-06 09:42:49 -0600 )edit

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

NickRos gravatar image NickRos  ( 2023-04-06 09:47:38 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2023-04-04 11:17:57 -0600

Seen: 1,512 times

Last updated: Apr 12 '23