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

Adding custom message to existing package (Humble + Ubuntu22) fails

asked 2023-01-26 06:45:34 -0600

AibohphobiA gravatar image

Hi there, I am trying to integrate an existing functioning package with a custom message(so far it has been divided to two projects).

I am failing when trying to modify the CMakeLists with the following interface:

rosidl_generate_interfaces(${PROJECT_NAME} 
  ${msg_files} 
)

I am getting the following error:

CMake Error at /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:213 (add_custom_target):
  add_custom_target cannot create target "myPackage" because another target
  with the same name already exists.  The existing target is a shared library
  created in source directory "/home/user/ros2_ws/src/inuros2".  See
  documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
  CMakeLists.txt:110 (rosidl_generate_interfaces)

My Cmake up to that point is as follows:

cmake_minimum_required(VERSION 3.8)

project(myPackage)

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(cv_bridge REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(rosidl_default_generators REQUIRED)

set (SOURCES
       ...
        )

set (INCLUDES
       ...
        )

add_library(${PROJECT_NAME} SHARED
        ${INCLUDES}
        ${SOURCES}
        )

set(dependencies
        cv_bridge
        rclcpp
        ...
        )

ament_target_dependencies(${PROJECT_NAME}
        ${dependencies}
        )

rclcpp_components_register_node(${PROJECT_NAME}
  PLUGIN "Class::NodeName"
  EXECUTABLE MyPackageExecutable
)

set(msg_files
  "msg/Features.msg"
)

rosidl_generate_interfaces(${PROJECT_NAME}
  ${msg_files}
)

Any help would be great.

Thanks!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2023-01-26 18:33:29 -0600

Generally speaking, you should not put messages in the same package as the code using it as a best practice.

... but if you want to, you need to add a few extra lines https://github.com/cra-ros-pkg/robot_... (so make sure to build the messages before the libraries).

edit flag offensive delete link more
0

answered 2023-02-02 03:34:00 -0600

AibohphobiA gravatar image

By using @stevemacenski 's example project, I was able to solve the issue. I basically generated two projects from the same package and Cmake. I am aware it's not best practice - BUT I had to do it because I also had to pack it into a debian(Which can only pack one project).

Following is a template for such a project if anyone needs it in the future:

find_package(rosidl_default_generators REQUIRED)
...

set(library_name mynode)

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

add_library(${library_name} SHARED
        ${INCLUDES}
        ${SOURCES}
        )

rosidl_get_typesupport_target(cpp_typesupport_target "${PROJECT_NAME}" "rosidl_typesupport_cpp")
target_link_libraries(${library_name} "${cpp_typesupport_target}")

ament_target_dependencies(${library_name}
${dependencies}
)

target_link_libraries(${library_name}
...
        )

install(TARGETS ${library_name} 
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)

rclcpp_components_register_node(${library_name}
  PLUGIN "Myclass::ActiveNode"
  EXECUTABLE activenode
)

target_include_directories(activenode PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>
...
        )
target_compile_features(activenode PUBLIC c_std_99 cxx_std_17)  # Require C99 and C++17


## Specify libraries to link a library or executable target against
target_link_libraries(activenode
...
        )

ament_target_dependencies(activenode
${dependencies}
)

  # Install binaries
install(TARGETS activenode
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)

# Install headers
install(
DIRECTORY include/
DESTINATION include
)

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

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

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

ament_export_include_directories(include)
ament_export_libraries(${library_name})
ament_export_dependencies(${dependencies} )

ament_package()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2023-01-26 06:45:34 -0600

Seen: 1,375 times

Last updated: Feb 02 '23