How to include external library (.so) into ROS2 environment
Hello there!
My development environment consists of ROS2 foxy and Ubuntu 18.04.
I have ROS2 package (my_ros2_pkg
) with ROS2 node (my_ros2_node
). Inside the ROS2 node, I want to use an external C library (not some external ROS2 package).
I used the following approach to bring the library code from the Git as well as to properly install the library. Here is the excerpt of CMakeLists.txt
file of my_ros2_pkg
:
include(ExternalProject)
ExternalProject_Add(my_ext_library
GIT_REPOSITORY https://github.com/my_ext_library.git
GIT_TAG master
SOURCE_DIR ${CMAKE_BINARY_DIR}/my_lib/src
BINARY_DIR ${CMAKE_BINARY_DIR}/my_lib/build
CONFIGURE_COMMAND cmake ${CMAKE_BINARY_DIR}/my_lib/src
BUILD_COMMAND make
INSTALL_COMMAND make install DESTDIR=${CMAKE_BINARY_DIR}/my_lib/install
)
include_directories("${CMAKE_BINARY_DIR}/my_lib/install/usr/local/include")
link_directories("${CMAKE_BINARY_DIR}/my_lib/install/usr/local/lib")
As a consequence, the source code of the external library is properly downloaded, the library is properly built and installed. .so
files of the library can be found under ros2_workspace/build/my_ros2_pkg/my_lib/install/usr/local/lib
However, when I try to run my_ros2_node
with
ros2 run my_ros2_pkg my_ros2_node
I have the following ROS2 error:
/home/ros2_workspace/install/my_ros2_pkg/lib/my_ros2_pkg/my_ros2_node: error while loading shared libraries: my_lib.so.1: cannot open shared object file: No such file or directory
It seems that ROS2 looks for the shared object (.so
) library files under ros2_workspace/install/my_ros2_pkg/lib/my_ros2_pkg
folder and not under ros2_workspace/build/my_ros2_pkg/my_lib/install/usr/local/lib
where they actually reside.
How can I fix this issue and properly include the path to the .so
files into the ROS2 environment? My CMakeLists.txt
file of my_ros2_pkg
also contains this line:
add_dependencies(my_ros2_node my_ext_library)
but to no avail. Do you have any idea what I am missing here? Thank you very much for your time and efforts.
Sincerely, Bojan.
I'm no cmake expert, But I've done my fair share of making some
_vendor
type ros2 packages.. (1 is too many for me..)You could try something like:
Or if that doesn't work try adding an additional
&& cp ......
stuff to yourINSTALL_COMMAND
..Good luck!
Hey, @ChuiV! Thanks for your answer. It seems that the
.so
files are expected in/usr/lib
location. When I moved the file there I was able to load the shared library. I will try what you suggest because I would like to download source code, build and install the library and use such installed.so
files from my ROS2 package.Cheers, Bojan.
In times past, I've made a dedicated
*_vendor
package (such as the sqlite3_vendor package. I didn't write that particular one, but I've used it as a template to make my own.) that wraps around the external project cmake.Then in regular ros2 packages, you can add dependencies on your "vendor" package, which should automatically add those libraries into the link path.
This whole process is quite a pain. I do with there was an easier way... But once it's done, it works pretty good.