Ask Your Question
1

Adding external c library to a ROS2 node

asked 2020-12-17 12:29:14 -0500

mamado gravatar image

I am using ROS2 foxy on a headless Ubuntu 20.04.1 LTS. I would like to use an external c library(Gstreamer library) in the node C++ code.

I included the the library in the node code as follows:

extern "C" {
#include "gst/gst.h"
}

In the package.xml file I added:

 <depend>libgstreamer1.0-dev</depend>
  <depend>libgstreamer-plugins-base1.0-dev</depend>

And in the CMakeList.txt file I added:

find_package(PkgConfig)
pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0
                    gstreamer-app-1.0)
add_executable(server src/my_server.cpp)
ament_target_dependencies(server
    rclcpp my_interfaces "${GSTREAMER}")

However, every time I run colcon build I get

fatal error: gst/gst.h: No such file or directory
    5 | #include "gst/gst.h"
      |          ^~~~~~~~~~~

gst.h can be found in /usr/include/gstreamer-1.0/gst

How can I link against this library and what is the best practice for adding external libraries in ROS2 in general. Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-12-18 13:05:51 -0500

jacobperron gravatar image

ament_target_dependencies() is only intended to be used with other ament packages.

For external packages, you should use the regular CMake facilities. For example, try changing your CMakeLists.txt to:

find_package(PkgConfig)
pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0
                    gstreamer-app-1.0)
add_executable(server src/my_server.cpp)
ament_target_dependencies(server
    rclcpp my_interfaces)
target_include_directories(server PUBLIC
 "${GSTREAMER_INCLUDE_DIRECTORIES}")
target_link_libraries(server
 "${GSTREAMER_LIBRARIES}")
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-12-17 12:29:14 -0500

Seen: 816 times

Last updated: Dec 18 '20