1

I use gcc version compiler in Ubuntu 18.04.3 LTS. ROS libraries are used. My code goes like this very simple:

typedef unsigned char       boolean; 

#define TRUE    (boolean)(1)
#define FALSE   (boolean)(0)

With the above compiled, I am getting compiler errors:

error: missing binary operator before token "("
  #define TRUE    (boolean)(1)
                          ^

Usage of the above #define is in OS Library file I guess.

../../../M3N_BSW/bsw_pc/virtual/ros/librosc/XmlRpc/include/ros_lm_xml_value.h:80:6: note: in expansion of macro ‘TRUE’
---------------------------------------------------------------------
 #if (TRUE == ROS_LM_XML_VALUE_DOUBLE_ENABLE) /* [05] */
      ^~~~

However when I defined as below, no errors.

#define SOMETHING  ( ( uchar8 ) 1 )

Similarly, the keyword boolean is used in other places to declare variables and those are working fine. Refer below code.. And Usage place goes like this:

#define ROS_LM_XML_VALUE_DOUBLE_ENABLE (FALSE)
someFunc()
{
     boolean someVar=FALSE;  //How come this is recognized correctly ? 
     #if (TRUE == ROS_LM_XML_VALUE_DOUBLE_ENABLE)
     {
        //Do something
     }
}

Usage of keyword boolean with #define goes wrong somewhere. Please guide me.

The same code is used in INTEGRITY OS. No compiler errors.

Vijay
  • 61
  • 1
  • 8
  • Probably the error comes from some code attempting to use `TRUE`, not just the `#define` all alone. Can you determine and show a line like that? – aschepler Jul 28 '20 at 02:46
  • @aschepler Thanks for asking. I added the usage in the question. Please do refer and provide your valuable feedback. – Vijay Jul 28 '20 at 02:53
  • And `boolean` is not a keyword. Do you know how it's actually defined? – aschepler Jul 28 '20 at 02:59
  • typedef unsigned char boolean; – Vijay Jul 28 '20 at 03:44
  • I'm guessing the second set of parentheses get interpreted as a function call, – tripleee Jul 28 '20 at 05:53
  • Yeah, the preprocessor doesn't know anything about typedefs, so the `#if` just replaces `boolean` with `0`, and `(0)(1)` is invalid. – aschepler Jul 28 '20 at 11:49
  • Possible duplicate of https://stackoverflow.com/questions/21338385/what-does-the-compiler-error-missing-binary-operator-before-token-mean . – Tyrel Kostyk Dec 07 '20 at 19:39

3 Answers3

1

It seems we cannot compare like this because TRUE in LHS is typecasted one. This portion is problem.

#if (TRUE == ROS_LM_XML_VALUE_DOUBLE_ENABLE)

Thanks for the support.

Vijay
  • 61
  • 1
  • 8
0

Looks like you're getting into an edge case of some kind. My suggestion would be to keep things as simple as possible.

Here's another article that talks about macros. Using boolean values in C

Another tactic with Macros debugging is to manually substitute the definition into the code and see if something more helpful bubbles up. It's very common for one issue to mask another issue. This defect inception can be difficult to track down.

The #define SOMETHING ( ( uchar8 ) 1 ) example appears to be casting the value 1 as a uchar8.

The #define TRUE (boolean)(1) seems to be initializing a boolean datatype with the value 1.

user3112728
  • 395
  • 1
  • 12
  • Added the usage portion code for your reference in the main question. Please check and provide your valuable feedback. – Vijay Jul 28 '20 at 04:02
0

My answer is sourced from another (more general) SO Answer: https://stackoverflow.com/a/21338386/9006042

The missing binary operator before token "(" error occurs when the preprocessor encounters invalid syntax while trying to evaluate an expression in a #if or #elif directive. So you're correct in your submitted answer that the error is coming from the #if (TRUE == ROS_LM_XML_VALUE_DOUBLE_ENABLE) line.

The underlying issue however is that you're attempting to typecast the macro that you're evaluating, which isn't allowed in #if directives. See all of the valid #if syntax rules here.

I would advise simply defining TRUE as (1) (and FALSE as (0)), instead of typecasting it to an unsigned char.

Tyrel Kostyk
  • 506
  • 1
  • 5
  • 11