How to include a srv from another pkg in ROS2 - fatal error: No such file or directory
I have followed the basic srv example for ROS2 from the official website. However, i tried to create myself the service instead of using example_interfaces (intern from ROS2). I first tried to create the service inside my pkg code (as in ROS1) but i couldnt include it correctly. So i tried to create the service in an extern package (i know its the best practice in ROS2) but i got the same problem when including. My srv file is Suma.srv, inside dev_ws/src/custom_pkg/srv. I will appreciate the solution either for include my inside srv file either for the external srv pkg. My srv pkg can compile correctly but the one with the code cant. Here i share my custom_pkg with the srv:
cmake_minimum_required(VERSION 3.5)
project(custom_interfaces)
# 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(rosidl_default_generators REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME}
"srv/Suma.srv"
)
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()
ament_export_dependencies(rosidl_default_runtime)
ament_package()
Now i show the package.xml:
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>custom_interfaces</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="pedro@todo.todo">pedro</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<buildtool_depend>rosidl_default_generators</buildtool_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Then, i will show my cpp_srvcli package (it works with the predetermined pkg example_interfaces): CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(cpp_srvcli)
# 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)
find_package(custom_interfaces REQUIRED)
find_package(example_interfaces REQUIRED)
# find_package(rosidl_cmake REQUIRED)
# find_package(rosidl_default_generators REQUIRED)
# rosidl_generate_interfaces(${PROJECT_NAME}
# "srv/Suma.srv"
# )
# include_directories(include)
#ament_export_dependencies(rosidl_default_runtime)
#include_directories(include ${Boost_INCLUDE_DIRS})
add_executable(server
src/add_two_ints_server.cpp
#src/main_server.cpp
)
add_executable(client
src/add_two_ints_client.cpp
# src/main_client.cpp
)
# rosidl_target_interfaces(server
# ${PROJECT_NAME} "rosidl_typesupport_cpp")
# rosidl_target_interfaces(client
# ${PROJECT_NAME} "rosidl_typesupport_cpp")
ament_target_dependencies(server custom_interfaces example_interfaces rclcpp)
ament_target_dependencies(client custom_interfaces example_interfaces rclcpp)
install(TARGETS
server
client
DESTINATION lib/${PROJECT_NAME}
)
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 ...
I have already found the problem. Although the srv name file must start with a capital letter, when you use the include inside the file.cpp you have to include starting with lowercase letter:
#include "cpp_srvcli/srv/suma.hpp"
Can you check each step with this tutorial?