I have the following ROS workspace structure:

└── ros_ws
    ├── package_a (C++)
    ├── package_b (C++)
    │   └── src
    │       └── clientNode
    └── package_c (Python)
        ├── src
        │   └── serverNode
        └── srv
            └── SumTwoInt.srv

Now I have tho build them all together, so I will have a CMakeLists.txt for every package, in particular the node in the package_c will be the server node for the service SumTwoInt. Furthermore I need the service SumTwoInt.srv to be called from the nodes in the others package as clients node. How can I retrive this service i.e., SumTwoInt, that is built in the shared enviroment?

I'm trying with #include <package_c/SumTwoInt.hpp> or <package_c/srv/SumTwoInt.hpp> but I can't find it. What I'm missing? There is any best practice to share services that i'm not following?

In the CMakeLists.txt of the package_c, I'm using:


I'm working with ROS 2 on Ubuntu 22.04.


1 Answer 1


Ideally, you build one package that is dedicated to storing all your custom message types(these can be msgs, srvs, or actions). If you take a look at this particular repository, you will see that the messages are defined in their own package, and the service server and client implementations are done in separate packages, where it is needed.

So, if you were to do this, you would have to restructure your workspace structure like the following:

   -package a (C++)
   -package_b (C++)
   -package_c (python)

I have added a new package called package_msgs and populated it with the srv file. I have also assumed that the Client side implementation for the SumTwoInt service is done in package_c.

If you want help on creating custom messages/services, I will suggest you take a look at this link.

EDIT: It could also have been the case that you have forgotten to declare that package_b depended on package_c in the CMakeLists.txt or the package.xml.

  • $\begingroup$ Many thanks for the answer. But once that is done, how can I recall this services in the server node script and in the client node script? Is there any difference if i'm working with rclpy or rclcpp? I Edit consequentially my comment, I will check the lin on custom services, many thanks again! $\endgroup$
    – Donca
    Oct 6, 2023 at 7:59
  • $\begingroup$ Once that is done, and you are able to see your service message properly by doing ros2 interface show <package_msgs>/srv/SumTwoInt, you should be able to use the service in your C++ node as #include "<package_msgs>/srv/sum_two_int.hpp and in your Python node as from <package_msgs>.srv import SumTwoInt. $\endgroup$
    – ssarkar
    Oct 6, 2023 at 8:02
  • $\begingroup$ And, If I can ask you again just to try to figure out the logic, these services are build in a shared enviroment after the colcon build? So they are always accessible? And have I a version of them in .hpp and one in .py, to be readable for both the scripts? Thank you again $\endgroup$
    – Donca
    Oct 6, 2023 at 8:06
  • $\begingroup$ That is a very good question to ask. What happens when you compile this msgs or interfaces as they are known in ROS 2, is that they generate IDL files in the install directory. You can see them by going to install/<package_msgs>/share/<package_msgs>/srv. You will see an IDL file generated for each of the messages/services/actions you have. From here, a bit of black magic allows us to generate a header file for cpp, and a class for Python. $\endgroup$
    – ssarkar
    Oct 6, 2023 at 8:40
  • 1
    $\begingroup$ ssarkar - while I agree that your answer is a best practice, it does not address the reason the OP had an issue. Given what we were told, their original package organization should be workable. They probably failed to declare that package_b depended on package_c in the CMakeLists.txt or package.xml file. $\endgroup$
    – Mike973
    Oct 6, 2023 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.