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

Creating ROS2 components with external library

asked 2022-08-04 11:02:08 -0500

Cryoschrome gravatar image

updated 2022-08-05 07:51:15 -0500

Hi, I am trying to create ros2_control hardware_interface using serial communication. I am using this library for serial communication. While building the package I get the following error


/usr/bin/ld: /home/cryos/workspaces/grab_ws/install/serial/lib/libserial.a(unix.cc.o): warning: relocation against `_ZTVN6serial15SerialExceptionE' in read-only section `.text._ZN6serial15SerialExceptionD2Ev[_ZN6serial15SerialExceptionD5Ev]'
/usr/bin/ld: /home/cryos/workspaces/grab_ws/install/serial/lib/libserial.a(serial.cc.o): relocation R_X86_64_PC32 against symbol `_ZTVN6serial6SerialE' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
 

Here is my CMakeList.txt


cmake_minimum_required(VERSION 3.8)
project(test_hardware_interface)

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(rclcpp_lifecycle REQUIRED)
find_package(lifecycle_msgs REQUIRED)
find_package(pluginlib REQUIRED)
find_package(hardware_interface)
find_package(serial REQUIRED)

add_library(
  ${PROJECT_NAME}
  SHARED
  src/test_joint_hardware_interface.cpp
)
target_include_directories(
  ${PROJECT_NAME}
  PUBLIC
  $<build_interface:${cmake_current_source_dir} include="">
  $<install_interface:include>
)
ament_target_dependencies(
  ${PROJECT_NAME}
  rclcpp
  hardware_interface
  lifecycle_msgs
  pluginlib
  rclcpp_lifecycle
  serial
)
pluginlib_export_plugin_description_file(hardware_interface joint_hardware_plugin_description.xml)

install(
  DIRECTORY include/
  DESTINATION include
)

install(
  TARGETS
  ${PROJECT_NAME}
  EXPORT export_${PROJECT_NAME}
  RUNTIME DESTINATION bin
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
)

ament_export_include_directories(
  include
)

ament_export_libraries(
  ${PROJECT_NAME}
)
ament_export_targets(
  export_${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_package()
 

I have created a node using the library and it works well and facing this issue while building the component.
Could you please explain the error and possible solution.

Any help is appreciated!
Thank you!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-08-05 21:03:33 -0500

ravijoshi gravatar image

You need to add rclcpp_components as one of the dependencies inside ament_target_dependencies. Please do the following:

1) First get the package

find_package(rclcpp_components REQUIRED)

2) Then add it to ament_target_dependencies

ament_target_dependencies(
  ${PROJECT_NAME}
  rclcpp
  rclcpp_components
  hardware_interface
  lifecycle_msgs
  pluginlib
  rclcpp_lifecycle
  serial
)

I am sharing an example for more info. Please feel free to check the ros2/demos/composition.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-08-04 11:02:08 -0500

Seen: 441 times

Last updated: Aug 05 '22