Warning: "extra ; ignored" by using C macros with ARMCC

799 views Asked by At

My compiler raises the warning #381-D: extra ";" ignored in such a situation:

I have a struct defined, like the following

struct example_s
{
  u8_t foo;
  SOME_MACRO(bar);
};

The macro SOME_MACRO(x) does the following:

#if defined(SYSTEM_A)
  #define SOME_MACRO(x) u16_t x##something
#else
  #define SOME_MACRO(x)    /* nothing */
#endif

Of course, the warning is correct, when SYSTEM_A is not defined. Simply because I have now a ; within the struct. But does someone know a way to avoid it correctly? I don't want to break the typical C-style by moving the ; into the macro.

3

There are 3 answers

0
undur_gongor On BEST ANSWER

You can add an unnamed 0-width bitfield instead:

#if defined(SYSTEM_A)
  #define SOME_MACRO(x) u16_t x##something
#else
  #define SOME_MACRO(x)    unsigned :0
#endif
1
arash kordi On

you can also insert an empty anonymous struct :

#if defined(SYSTEM_A)
  #define SOME_MACRO(x) u16_t x##something
#else
  #define SOME_MACRO(x)    struct {}
#endif
4
Paul R On

One way that is a bit of a kludge but it seems to work with gcc:

#if defined(SYSTEM_A)
  #define SOME_MACRO(x) u16_t x##something
#else
  #define SOME_MACRO(x) int x[0]   /* nothing */
#endif

With this method you end up with a struct like this:

struct example_s
{
  u8_t foo;
  int bar[0];
};

which has the correct size (i.e. as size as if bar had not been defined at all).