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

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

I have a structure defined as follows:

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

The SOME_MACRO(x) macro performs the following actions:

 #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 if SYSTEM_A not defined. Just because I have now ; inside the structure. But does anyone know a way to avoid this correctly? I do not want to break the typical C-style by moving ; into the macro.

+4
source share
3 answers

Instead, you can add an unnamed bit field of width 0:

 #if defined(SYSTEM_A) #define SOME_MACRO(x) u16_t x##something #else #define SOME_MACRO(x) unsigned :0 #endif 
+3
source

One way, which is a bit kludge, but 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 

Using this method, you will get the following structure:

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

which has the correct size (that is, the size as if bar had not been defined at all).

+4
source

you can also insert an empty anonymous structure:

 #if defined(SYSTEM_A) #define SOME_MACRO(x) u16_t x##something #else #define SOME_MACRO(x) struct {} #endif 
+3
source

Source: https://habr.com/ru/post/1500492/


All Articles