4

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.

daniel
  • 85
  • 4

3 Answers3

4

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).

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 1
    Please note that this is not valid in ISO C (array size constant "... shall have a value greater than zero"). – undur_gongor Sep 04 '13 at 10:51
  • @undur: thanks - I hadn't noticed the question was tagged as C and had wrongly assumed C++ - but it does seem to work with gcc at least when compiled as either C or C++. I'll leave it to the language lawyers and pedants to down-vote as they see fit. ;-) – Paul R Sep 04 '13 at 11:06
  • 2
    The ARMCC raises the error: "The size of an array must be greater than zero" – daniel Sep 04 '13 at 11:12
  • @daniel: OK - sorry about that - hopefully one of the other solutions will work for you. I'll leave this answer here even though it doesn't work for ARMCC in case it's of interest to anyone with a similar problem on a different compiler or with C++. – Paul R Sep 04 '13 at 11:25
3

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
arash kordi
  • 2,470
  • 1
  • 22
  • 24
  • 1
    ARMCC raises the error "Expected a declaration". Unfortunately, also using the statement `struct test {}` – daniel Sep 04 '13 at 11:22
3

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
undur_gongor
  • 15,657
  • 5
  • 63
  • 75