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

How do I test the ROS version in C++ code?

asked 2011-03-25 15:50:35 -0600

joq gravatar image

updated 2012-06-14 01:02:13 -0600

I maintain a package that needs to support both cturtle and diamondback, at least for a while. It includes <roslib/Clock.h>, which is deprecated in diamondback. That generates a compiler warning. I prefer to reserve compiler warnings for actual bugs in my code.

How can I #ifdef this code so it includes <roslib/Clock.h> in cturtle, but uses <rosgraph_msgs/Clock.h> for diamondback?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
7

answered 2011-03-25 18:34:34 -0600

tfoote gravatar image

In the roscpp package roscpp/include/ros/common.h has the following defines:

#define ROS_VERSION_MAJOR 1
#define ROS_VERSION_MINOR 4
#define ROS_VERSION_PATCH 5
#define ROS_VERSION_COMBINED(major, minor, patch) (((major) << 20) | ((minor) << 10) | (patch))
#define ROS_VERSION ROS_VERSION_COMBINED(ROS_VERSION_MAJOR, ROS_VERSION_MINOR, ROS_VERSION_PATCH)

#define ROS_VERSION_GE(major1, minor1, patch1, major2, minor2, patch2) (ROS_VERSION_COMBINED(major1, minor1, patch1) >= ROS_VERSION_COMBINED(major2, minor2,\
 patch2))
#define ROS_VERSION_MINIMUM(major, minor, patch) ROS_VERSION_GE(ROS_VERSION_MAJOR, ROS_VERSION_MINOR, ROS_VERSION_PATCH, major, minor, patch)
edit flag offensive delete link more

Comments

If you have installed ROS through standard way on Linux, common.h is located here: /opt/ros/{ROS_DISTRO}/include/ros/common.h

Keivan gravatar image Keivan  ( 2018-06-28 03:01:52 -0600 )edit

This include file doesn't seem to exist in ROS 2 distributions (e.g., I searched on rolling, galactic and foxy). What are the alternatives?

Olivier Michel gravatar image Olivier Michel  ( 2022-04-26 05:16:13 -0600 )edit
7

answered 2011-06-16 06:50:19 -0600

baxelrod gravatar image

To expand on tfoote's answer, here is an example:

#if ROS_VERSION_MINIMUM(1, 4, 5) // if current ros version is >= 1.4.5
        // Diamondback code
#else
        // CTurtle code
#endif
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2011-03-25 15:50:35 -0600

Seen: 3,220 times

Last updated: Jun 16 '11