> -----Original Message----- > From: > avr-gcc-list-bounces+eweddington=cso.atmel....@nongnu.org > [mailto:avr-gcc-list-bounces+eweddington=cso.atmel....@nongnu. > org] On Behalf Of Anton James Erasmus > Sent: Wednesday, January 28, 2009 11:48 PM > To: avr-gcc-list@nongnu.org > Subject: [avr-gcc-list] Compiler bug or Incorrect C ? > > Hi, > > I have the following C code which gives the compiler > error. > "error: initializer element is not a constant" > > #define COMPILE_HOUR (((__TIME__[0]-'0')*10) + > (__TIME__[1]-'0')) > #define COMPILE_MINUTE (((__TIME__[3]-'0')*10) + > (__TIME__[4]-'0')) > #define COMPILE_SECOND (((__TIME__[6]-'0')*10) + > (__TIME__[7]-'0')) > > const unsigned char gCompileHour=COMPILE_HOUR; > const unsigned char gCompileMinute=COMPILE_MINUTE; > const unsigned char gCompileSecond=COMPILE_SECOND; > > but the following code compiles without problems and one > can see in the assembly generated that the macros do > collapse to a single constant. > > #define COMPILE_HOUR (((__TIME__[0]-'0')*10) + > (__TIME__[1]-'0')) > #define COMPILE_MINUTE (((__TIME__[3]-'0')*10) + > (__TIME__[4]-'0')) > #define COMPILE_SECOND (((__TIME__[6]-'0')*10) + > (__TIME__[7]-'0')) > > > unsigned char GetCompileHour(void) > { > unsigned char hour=COMPILE_HOUR; > return(hour); > } > > unsigned char GetCompileMinute(void) > { > unsigned char minute=COMPILE_MINUTE; > return(minute); > } > > unsigned char GetCompileSecond(void) > { > unsigned char second=COMPILE_SECOND; > return(second); > } > <snip> > So is the code that gives an error actually incorrect C, or > is it a compiler bug ? >
IMO, incorrect C. Anything that produces code has to go into a function; it cannot be outside of a function. However, an argument can be made that your macros should resolve to be a constant, therefore don't require using a function. But how you are using it, a builtin preprocessor string that you are using like any ordinary string stored in data... I wouldn't be surprised that the compiler might throw a fit about it. Perhaps it does create code, and only through the optimization process does it finally resolve to a constant, therefore it would require being in a function. Anyway, my 2 cents. _______________________________________________ AVR-GCC-list mailing list AVR-GCC-list@nongnu.org http://lists.nongnu.org/mailman/listinfo/avr-gcc-list