Amanda Nascimento
Amanda Nascimento

Reputation: 1

I can't add my custom ROS2 Humble messages to my code

So, I'm new to CMake and ROS2. I'm having trouble getting results from the ros2 tutorials for custom messages. What's there isn't working for me and I don't understand why. The code is from my work and I can't publish it, so I'll post an example I'm making so that if it works I can just replicate it.

My problem is that I can't use the generated messages for my code. It says it can't find the header files, but when I go to look in the build/ros_generator_c/test directory, the test.hpp header is there; the header was generated.

If it's helpful, I'm using VSCode with the c++ extensions, cmake, cmaketools, ros extension and I'm on Ubuntu 22.04.

I have this structure directory:

teste
-- example
  -- teste.cpp
-- include/teste
  -- lib_teste.hpp
-- msg
  -- Teste.msg
-- src
  -- lib_teste.cpp
CMakeLists.txt
package.xml

My CMakeList.txt is like this:

cmake_minimum_required(VERSION 3.8)
project(teste)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(rclcpp REQUIRED)

rosidl_generate_interfaces(
  ${PROJECT_NAME}
  "msg/Teste.msg"
)  

rosidl_get_typesupport_target(cpp_typesupport_target
  ${PROJECT_NAME} rosidl_typesupport_cpp)

add_library(LibTeste
  src/lib_teste.cpp
)

target_include_directories(LibTeste
    PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
    $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
)

ament_target_dependencies(LibTeste rclcpp rosidl_generator_cpp)

install(
    DIRECTORY include/
    DESTINATION include
)

install(
    TARGETS LibTeste
    EXPORT export_LibTeste
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
    INCLUDES DESTINATION include
)

ament_export_targets(export_LibTeste HAS_LIBRARY_TARGET)

ament_export_dependencies(rclcpp)

add_executable(Teste example/teste.cpp)
ament_target_dependencies(Teste rclcpp)

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

target_link_libraries(Teste LibTeste "${cpp_typesupport_target}")

target_include_directories(Teste PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

target_compile_features(Teste PUBLIC c_std_99 cxx_std_17)

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

ament_export_dependencies(rosidl_default_runtime)

ament_package()

And my package.xml is like this:

<?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>teste</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="[email protected]">amandarln</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>
  <buildtool_depend>rosidl_default_generators</buildtool_depend>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <exec_depend>rosidl_default_runtime</exec_depend>

  <member_of_group>rosidl_interface_packages</member_of_group>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

My codes:

lib_teste.hpp (yes, just one call because I want to test just that)

#include "teste/msg/teste.hpp"

lib_teste.cpp

#include <teste/lib_teste.hpp>

namespace Testando
{
    int Testando::getTeste()
    {
        return 1;
    }
}

teste.cpp

#include <teste/lib_teste.hpp>

int main()
{
    teste::msg::Teste teste;
    
return 0;
}

And my message error:

colcon build --packages-select teste
Starting >>> teste   
--- stderr: teste                             
In file included from /home/amandarln/ff2_ws/src/teste/src/lib_teste.cpp:1:
/home/amandarln/ff2_ws/src/teste/include/teste/lib_teste.hpp:1:10: fatal error: teste/msg/teste.hpp: No such file or directory
    1 | #include "teste/msg/teste.hpp"
      |          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/LibTeste.dir/build.make:76: CMakeFiles/LibTeste.dir/src/lib_teste.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:594: CMakeFiles/LibTeste.dir/all] Error 2
gmake[1]: *** Waiting for unfinished jobs....
gmake: *** [Makefile:146: all] Error 2
---
Failed   <<< teste [0.11s, exited with code 2]

Summary: 0 packages finished [0.30s]
  1 package failed: teste
  1 package had stderr output: teste

I tried adding the following lines as requested in the ROS2 humble tutorial

target_include_directories(LibTeste
    PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>)
target_link_libraries(Teste LibTeste "${cpp_typesupport_target}")

Upvotes: 0

Views: 180

Answers (0)

Related Questions