Ask Your Question
0

Create ROS2 package from shared library (.so) and link it to node

asked 2020-07-30 04:24:30 -0500

Matous Pokorny gravatar image

updated 2020-07-30 04:25:24 -0500

Hello guys!

I try prepare the ROS2 package containing just a shared library in binary form (.h and .so) and than link it with my node. I think the library package works - I can see the .so files and .h file in the install directory. I am little bit lost at the node side. Linker says undefined reference error. I follow the ament_cmake documentation (https://index.ros.org/doc/ros2/Tutori...) but it is maybe little bit outdated. Documentation recommend use ament_export_interfaces, but I received warning it is deprecated way.

I am also a little bit confused with the linking debugging. There is no verbose output at top the top level (colcon). There is a colcon log in /log directory, but it is not what i want. I will try give the verbose parameter to ld using target_compile_options.

Here is my library stuff.

Package

<?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>epos_library</name>
  <version>0.0.1</version>
  <description>Support package for interfacing EPOS motor control unit</description>
  <maintainer email="matous.pokorny@datavision.software">Matous Pokorny</maintainer>
  <license>none</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

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

CMakeLists

cmake_minimum_required(VERSION 3.5)
project(epos_library)

# 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)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

# find the 32 or 64 bit libraries
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  set(ARCH "x86_64")
else()
  set(ARCH "x86")
endif()
message(STATUS "Detected architecture: ${ARCH}")

include_directories(include)

set(ftd2xx_LIBRARY ${PROJECT_SOURCE_DIR}/lib/${ARCH}/libftd2xx.so.1.4.6)
set(EposCmd_LIBRARY ${PROJECT_SOURCE_DIR}/lib/${ARCH}/libEposCmd.so.6.4.1.0)

add_library(ftd2xx SHARED ${ftd2xx_LIBRARY})
set_target_properties(ftd2xx PROPERTIES LINKER_LANGUAGE CXX)

add_library(EposCmd SHARED ${EposCmd_LIBRARY})
set_target_properties(EposCmd PROPERTIES LINKER_LANGUAGE CXX)

ament_export_targets(ftd2xx HAS_LIBRARY_TARGET)

ament_export_targets(EposCmd HAS_LIBRARY_TARGET)

install(
  DIRECTORY include/
  DESTINATION include
)

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

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

ament_export_include_directories(include)
ament_export_libraries(ftd2xx EposCmd)

ament_package()

Here is my node stuff.

Package

<?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>cart2</name>
  <version>0.0.1</version>
  <description>package for a moving program for a robot</description>
  <maintainer email="matous.pokorny@datavision.cz">Matous Pokorny</maintainer>
  <license>none</license>

  <buildtool_depend>ament_cmake</buildtool_depend>
  <build_depend>epos_package</build_depend>
  <exec_depend>rclcpp</exec_depend>
  <exec_depend>std_msgs</exec_depend>
  <exec_depend>epos_package</exec_depend>

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

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

CMakeLists

cmake_minimum_required(VERSION 3.5)
project(cart2)

# 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 ...
(more)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2020-07-30 12:37:42 -0500

jacobperron gravatar image

As of Foxy Fitzroy ament_export_interfaces is deprecated, replaced with ament_export_targets. The documentation was outdated and has been updated in https://github.com/ros2/ros2_document....

There's a recommendation in the tutorial to use a different EXPORT name than the library:

The EXPORT notation of the install call requires additional attention: It installs the CMake files for the my_library target. It is named exactly like the argument in ament_export_interfaces and could be named like the library. However, this will then prohibit using the ament_target_dependencies way of including your library. To allow for full flexibility, it is advised to prepend the export target with something like export_<target>.

I don't know if it solves your issue, but you could try making the following changes to your first CMakeLists.txt:

ament_export_targets(export_ftd2xx HAS_LIBRARY_TARGET)

ament_export_targets(export_EposCmd HAS_LIBRARY_TARGET)

install(
  DIRECTORY include/
  DESTINATION include
)

install(
  TARGETS ftd2xx
  EXPORT export_ftd2xx
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include
)

install(
  TARGETS EposCmd
  EXPORT export_EposCmd
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include
)
edit flag offensive delete link more

Comments

Thank you for your reply. I tried to edit my CMakeLists.txt as you suggested but with no success.

Matous Pokorny gravatar image Matous Pokorny  ( 2020-08-13 03:30:01 -0500 )edit
0

answered 2020-07-30 06:41:12 -0500

Matous Pokorny gravatar image

updated 2020-07-30 07:31:14 -0500

Few more notes.

I have got inspiration from this repository https://github.com/RIVeR-Lab/epos_har... but it is ROS1 package.

I use foxy and Ubuntu 20.04 LTS.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-07-30 04:24:30 -0500

Seen: 44 times

Last updated: Jul 30