Example 1.6 Error with #include "rclcpp/rclcpp.hpp" My First ROS Program ROS2BasicConcepts

Following the first step of Example 1.6 I copy the code of simple.cpp and paste in in a new file. However I get this error that the file “rclcpp/rclcpp.hpp” is not found.

In addition if I continue and I run the colcon build The system gives a package failed: my_package.

Hi,

I just followed the notebooks instructions and seems to work without issues:

// Here we are including all the headers necessary to use the most common public pieces of the ROS system
// In this case we use the rclcpp client library, which provides a C++ Api for interacting with ROS
// Always, when we create a new C++ file, we will need to add this include:
#include "rclcpp/rclcpp.hpp"

// We start the main C++ program
int main(int argc, char * argv[])
{
  // We initiate the rclcpp client library
  rclcpp::init(argc, argv);
  // We initiate a ROS node called ObiWan
  auto node = rclcpp::Node::make_shared("ObiWan");

  // This is the same as a print in ROS
  RCLCPP_INFO(node->get_logger(), "Help me Obi-Wan Kenobi, you're my only hope"); 
  
  // We shutdown the rclcpp client library
  rclcpp::shutdown();
  // We end our program
  return 0;
}

And I added this code CmakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(my_package)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

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(rclcpp REQUIRED)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # uncomment the line when a copyright and license is not present in all source files
  #set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # uncomment the line when this package is not in a git repo
  #set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()


add_executable(simple_node src/simple.cpp)
ament_target_dependencies(simple_node rclcpp)

install(TARGETS
   simple_node
   DESTINATION lib/${PROJECT_NAME}
 )

install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)

ament_package()

Please check that you compiled and added the correct code. If it doesnt work please tell us agian.

source ~/ros2_ws/install/setup.bash
colcon build

It is still giving me the same error that it doesn’t find the #include “rclcpp/rclcpp.hpp” file.
I was able to do the colcon buil but when ros2 launch I get the following error:

user:~/ros2_ws$ ros2 launch my_package my_package_launch_file.launch.py
[INFO] [launch]: All log files can be found below /home/user/.ros/log/2021-03-16-09-48-47-544838-2_xterm-5411
[INFO] [launch]: Default logging verbosity is set to INFO
Task exception was never retrieved
future: <Task finished name='Task-2' coro=<LaunchService._process_one_event() done, defined at /opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py:271> exception=SubstitutionFailure("executable 'simple' not found on the libexec directory '/home/user/ros2_ws/install/my_package/lib/my_package' ")>
Traceback (most recent call last):
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py", line 273, in _process_one_event
    await self.__process_event(next_event)
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py", line 293, in __process_event
    visit_all_entities_and_collect_futures(entity, self.__context))
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures
    futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context)
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures
    futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context)
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures
    futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context)
  [Previous line repeated 1 more time]
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 38, in visit_all_entities_and_collect_futures
    sub_entities = entity.visit(context)
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/action.py", line 108, in visit
    return self.execute(context)
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch_ros/actions/node.py", line 442, in execute
    ret = super().execute(context)
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/actions/execute_process.py", line 775, in execute
    self.__expand_substitutions(context)
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/actions/execute_process.py", line 635, in __expand_substitutions
    cmd = [perform_substitutions(context, x) for x in self.__cmd]
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/actions/execute_process.py", line 635, in <listcomp>
    cmd = [perform_substitutions(context, x) for x in self.__cmd]
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/perform_substitutions_impl.py", line 26, in perform_substitutions
    return ''.join([context.perform_substitution(sub) for sub in subs])
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/perform_substitutions_impl.py", line 26, in <listcomp>
    return ''.join([context.perform_substitution(sub) for sub in subs])
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_context.py", line 184, in perform_substitution
    return substitution.perform(self)
  File "/opt/ros/foxy/lib/python3.8/site-packages/launch_ros/substitutions/executable_in_package.py", line 84, in perform
    raise SubstitutionFailure(
launch.substitutions.substitution_failure.SubstitutionFailure: executable 'simple' not found on the libexec directory '/home/user/ros2_ws/install/my_package/lib/my_package'

I found the error, It is because I modified the launch file so the executable will be named simple as I thought it was the name of the cpp file but I realised because of the CMakeList that it is renamed simple_node.

Even though it already works I still find the problem of the file and it gives me this red error at the side of the files that I showed in the first message.

Thank you for your help.