I am working on a project which should have to run on both ros melodic (ubuntu 18.04) and ros noetic(ubuntu 20.04). so while doing so I made an if-else statement in my code that,
if(distro=="noetic"){
...do this
else
...do this
The problem occurs basically, noetic and melodic support different versions of Point cloud Libraries(PCL) which also make them different in their way of initialization. So when I put PCL initialization (the way noetic support) in the if statement and in the else statement I put melodic way of initialization. I want the compiler( catkin_make command I use) to compile only the if statement but it also compiles else statement which gives an error because noetic did not support a melodic way of initialization. What should be the way?
CodePudding user response:
Use preprocessor macros. This needs to be handled before compile time. The preprocessor macros #ifdef
and #ifndef
will check if a token is present in the symbol table (check the documentation for your OS to see if there is a defined token for it), and skip to the according if/else section.
#ifdef NOETIC_DISTRO
//Code for noetic distro
#endif
#ifdef OTHER_DISTRO
//Code for other distro
#endif