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

using apt installed library in cmakelists

asked 2023-06-05 14:55:41 -0500

ARK3r gravatar image

This is a C++ package management question but I'm asking it here on ROS Answers considering ament might have its own way of doing things and this particular package that won't use ros components but will be built by colcon will be used by other ros2 packages. So ye, sorry about asking a c++ question here.

So I want to use libdmodbus (C code) that I've installed using apt package manager on ubuntu 22.04. I am running ros2 humble.

I can see the .so files are here:

$ ls /lib/x86_64-linux-gnu | grep modbus
libmodbus.so
libmodbus.so.5
libmodbus.so.5.1.0

I have a motor.cpp and a motor.hpp. I would like to include modbus into motor.hpp.

The Motor class inside motor.cpp will be itself a library, since this class is just an sdk for the robot and doesn't have any ros components beside the fact that colcon will be building it.

So my two questions are:

  1. What do I need to add to CMakeLists.txt so I can include modbus inside motor.hpp?
  2. How should I configure CMakeLists.txt so that I can include the motor class in another ros2 package?
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2023-06-06 09:39:32 -0500

130s gravatar image

updated 2023-06-07 13:13:22 -0500

Sounds like you're making your software as "ament-compatible package" or just "ament package" (*1) as you use colcon to build. colcon and ament are IINM made along with primarilly ROS2 framework in mind, but they are independent from ROS2, so using them for software that are not ROS-dependent is totally a valid usecase IMO, if that's what you're doing.

  1. What do I need to add to CMakeLists.txt so I can include modbus inside motor.hpp?

Adding Dependencies section in the tutorial is a good resource.

Going a little step-by-step. 1st, define dependencies that your cmake target needs to depend on.

set(DEPS
  ament_cmake
  rclcpp
  %YOUR_CHOICES%
)
foreach(dep IN ITEMS ${DEPS}) find_package(${dep} REQUIRED) endforeach()

Define a cmake target for your program. This line differs depending on whether you're making an executable or a library.

add_executable(%YOUR_TARGET% %PATH_TO_YOUR_CPP%)
add_library(%YOUR_TARGET% %PATH_TO_YOUR_CPP%)

Then, using the approaches explained in the tutorial, set dependency to your cmake target. The tutorial doesn't seem to clarify cases where either approach is more sutaible though, I'd say normally use ament_target_dependencies that does multiple things at once.

ament_target_dependencies(%YOUR_TARGET% ${DEPS})

(I experienced a case where I could only get my app working with target_link_libraries, which I asked in robotics.stackexchange.com#24837, fyi).

2 . How should I configure CMakeLists.txt so that I can include the motor class in another ros2 package?

For this question, I don't see much difference from the 1st one, so I assume this 2nd one is about "how to add dependency in "ament pkg" than just configuring CMakeLists.txt.

Defining dependency for ament package is 2-fold:


In your case, I'm not sure which apt/deb package you use for libmodbus, but in the rosdep key list on rosdistro, there's libmodbus-dev (rosdistro/rosdep/base.yaml#L4047) and libmodbus5 (note the link is permalink so the info you see may be outdated). If either of those are what you need, add it in your package.xml

*1...I haven't seen yet much of these expression in ament world though. Back in ROS1, "catkin package" was commonly mentioned in order to describe a software that is packaged using package.xml, CMakeLists.txt in the way Catkin defines.


UPDATE Inspired by this thread, I opened ros2_documentation!3719 to add more explanation in rosdep's tutorial. If anyone can review and leave comments there, that will help review.

edit flag offensive delete link more

Comments

Thank you so much this was very extensive and explained everything I needed to know.

I also checked out the pull request you made and it does really shed a new light on rosdep for me.

ARK3r gravatar image ARK3r  ( 2023-06-07 13:08:36 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2023-06-05 14:50:19 -0500

Seen: 963 times

Last updated: Jun 07 '23