How can I call a ROS-service that was neither written nor published by me from within C++ code?

I see the service listed when using rosservice list, which shows me

enter image description here

and I can call it easily by using rosservice call /airsim_node/SimpleFlight1/takeoff for instance.

Now, what I want, is to work with this service from within a C++ file (send request, receive response).

The documentation explains how to do this but requires you to have access to the service's source such as its compiled source code or even its *srv-file.

But I want to call an arbitrary service that is out of my scope. I, as a user, am able to do this via rosservice call, so I guess it should be simple to do the same via code?


1 Answer 1


rosservice call is written in Python, so it has access to a Python module containing the service types. If you were using Python you would be able to use the same module yourself.

However since you want to use C++, you instead need the header files that were generated from the service definition. There unfortunately is no way around this, as those header files and the library that goes with them contain a lot of information required to make the service work: the C++ types for the request and reply, how to serialise and deserialise those types for transport over the wire, etc.

If you can see the service available on your system using rosservice list and rosservice call works, then the Python module for the service is available. This implies that the C++ library and headers are also available. You haven't said what the type of your service is but the header file names follow a pattern of [package_name]/[TypeInCamelCase.h]. You will also need to link to the library containing the service's compiled support code in your CMakeLists.txt; this library name also follows a pattern of [package_name]_gencpp.

Once you know the header name, the library name, and the C++ type that represents the service (which you can get from the header file if you don't already know it), then the rest of your code is the same as in the tutorial you linked to.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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