Robotics StackExchange | Archived questions

Detect ROS version in compile time

Hi all, I am looking for a very simple question, but I can't find any other post about this topic, so I'm hoping someone can help me:

How can I detect in compile time which ROS distro I am running on? Ideally, I would like to have something like:

#ifdef MELODIC

#include "my_header_melodic.h"

#elif NOETIC

#include "my_header_noetic.h"

#endif

I am now doing this by hand forcing it in my CMakelists.txt with a add_definitions(-DNOETIC) or -DMELODIC, but I would like ideally to be this automatic.

Thank you in advance,

R.

Asked by ramonvp on 2021-03-01 08:18:21 UTC

Comments

I believe this is a duplicate of (at least) #q9562. For the CMake side: #q148831 (see the accepted answer).

Please see whether that helps, and if it does, please close your question as a duplicate.

Asked by gvdhoorn on 2021-03-01 10:53:36 UTC

Thanks, #q148831 was exactly what I was looking for! The way I'm using it is simply passing it on as a compiler flag: add_definitions(-D$ENV{ROS_DISTRO})

Asked by ramonvp on 2021-03-02 07:24:51 UTC

that wasn't exactly what I was thinking, but if you're happy, I'm happy.

(comparing versions will always be better than comparing for the existence of particular names)

Asked by gvdhoorn on 2021-03-02 07:53:07 UTC

Answers