ROS2 C++ custom library problem


I am trying to create a C++ library with some dependencies in ROS2.

I did this tutorial from the open classes :

Now I want to modify the library from the course.

But when I want to do a library which has some dependencies, I always get compiling errors:

This is how the source file looks:

This is how the header file looks:

This is how the CMakeList.txt looks:

cmake_minimum_required(VERSION 3.8)

  add_compile_options(-Wall -Wextra -Wpedantic)

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(std_msgs REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)

# let the compiler search for headers in the include folder

# define a library target called my_value_converter_library
add_library(my_value_converter_library src/my_value_converter_library.cpp) 

# this line to exports the library
ament_export_targets(my_value_converter_library HAS_LIBRARY_TARGET)

# install the include/my_value_converter_library directory to the install/include/my_value_converter_library
  DIRECTORY include/my_value_converter_library

  TARGETS my_value_converter_library
  EXPORT my_value_converter_library



And this is how the package.xml looks:

So does anyone know where I need to add the Dependencies, such that I can compile my library?

Thanks in advance for any help.


Did you watch that video to the end? Some modifications were made to the CMakeLists.txt later in the video, and I can’t see them in the one you posted here.

Hi @bayodesegun

Thank you for your response.
Yes I watched the video to the end.

In the video he does 2 things.
He does 1 package “my_value_converter_library” and another package “my_value_converter_node”.

In the first package he creates the library with the header file and in the second package he uses it.
Therefore the modification he does in the end, is just for the “my_value_converter_node” package CMakeList.txt.

But now I am just trying to change the “my_value_converter_library” package with adding a std_msgs/msg/float32 message.
And I am just trying to compile this modified “my_value_converter_library” package.

Therefore I don’t need to make there the changes, which he makes for the node-package.

So I am trying to create a library which has some dependencies, because the same issue I would get if my library for example depends on rclcpp or geometry_msg etc…

Therefore I would like to know, where do I need to add the dependencies in the CMakeList.txt for the library-package.

Also the code I am using, is the code from the open course, I downloaded it from the rosject. The only thing I did was adding the myFunction() with the std_msgs::msg::Float32 message.

Do you have any Idea where the std_msgs dependency needs to be added? I assume it should be possible.

Thanks in advance for any help.

Hi there,

I believe you are missing an ament_target_dependencies or since you are trying to build a library ament_export_depencies( my_value_converter_library std_msgs) in your CMakeLists.txt

Yes, you are right. I jumped to a conclusion too fast, thanks for correcting me.

What you could do to be bullet-proof-sure is to create the library with the needed dependencies from scratch (use could use another name and then compare the CMakeLists.txt and package.xml generated):

ros2 pkg create my_value_converter_library2 --build-type ament_cmake --license BSD-3-Clause --dependencies rclcpp std_msgs

Hi @tschuette and @bayodesegun

Thanks a lot this was the solution!

Adding these 2 lines:

# add dependencies
ament_target_dependencies(my_value_converter_library std_msgs)
ament_export_dependencies(my_value_converter_library std_msgs)

So it could compile now successfully!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.