ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

get the message definition in c++

asked 2012-12-12 08:51:45 -0500

andre__ gravatar image

updated 2012-12-13 02:50:55 -0500

Hi everyone,

i would like to get the ros message defintion file as a string in c++. Similar to how it is done in python ...

Where I just have to call:


and get the following output:

std_msgs/Header header
  uint32 seq
  time stamp
  string frame_id
float32[] axes
int32[] buttons

The message type is not known before compile time ... Thus, everything I have is a string in the following format

std::string strType = "sensor_msgs/Joy"; // or something else like = "std_msgs/String" ...

so i need a method or a function which takes in a string, defining the package and the message type ... and want to get out a string with the complete message definition ...

But like it is done in python, not only the content of sensor_msgs/msg/Joy.msg, which is:

Header header           # timestamp in the header is the time the data is received
float32[] axes          # the axes measurements from a joystick
int32[] buttons         # the buttons measurements from a joystick

I need the extracted format of all nested message definitions like presented in the first example ...

I think this is also possible somehow in roscpp, but I could not figure it out yet, thanks for any suggestions ...

Cheers ...

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-12-12 09:52:46 -0500

Miguel S. gravatar image

updated 2012-12-12 09:56:43 -0500

Just use ros::message_traits::Definition<Type>::value(). Following your example, to print the definition of sensor_msgs/Joy to the console, simply write:

std::cout << ros::message_traits::Definition<sensor_msgs::Joy>::value() << std::endl

This assumes you have the appropriate includes.

edit flag offensive delete link more


For this to be possible, all the message headers that will eventually be used should be available at compile time. I think an approach using strings only would be way more flexible and can cover all kinds of messages.

Mehdi. gravatar image Mehdi.  ( 2017-09-13 05:28:46 -0500 )edit

Question Tools

1 follower


Asked: 2012-12-12 08:51:45 -0500

Seen: 2,068 times

Last updated: Dec 13 '12