Create ROS2 package from shared library (.so) and link it to node
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 amentcmake documentation (https://index.ros.org/doc/ros2/Tutorials/Ament-CMake-Documentation/) but it is maybe little bit outdated. Documentation recommend use amentexport_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 targetcompileoptions.
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 -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(epos_library REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
add_executable(epos_motor_node src/epos_motor.cpp src/epos_motor_node.cpp)
target_include_directories(epos_motor_node PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
ament_target_dependencies(epos_motor_node rclcpp std_msgs epos_library)
#target_link_libraries(epos_motor_node rclcpp std_msgs epos_library)
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()
install(TARGETS
epos_motor_node
DESTINATION lib/${PROJECT_NAME}/)
ament_package()
Update (Jul 30 '20): Few more notes.
I have got inspiration from this repository but it is ROS1 package.
I use foxy and Ubuntu 20.04 LTS.
Asked by Matous Pokorny on 2020-07-30 04:24:30 UTC
Answers
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_documentation/pull/804.
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 themy_library
target. It is named exactly like the argument inament_export_interfaces
and could be named like the library. However, this will then prohibit using theament_target_dependencies
way of including your library. To allow for full flexibility, it is advised to prepend the export target with something likeexport_<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
)
Asked by jacobperron on 2020-07-30 12:37:42 UTC
Comments
Thank you for your reply. I tried to edit my CMakeLists.txt as you suggested but with no success.
Asked by Matous Pokorny on 2020-08-13 03:30:01 UTC
Comments
Hi!
Have you found a way to properly handle external non-ROS libraries in a ROS2 repository at the end?
Thanks, Cheers, GC
Asked by anthares on 2020-12-31 10:59:33 UTC