PreProcessor (CPP) Confusion

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

While answering a question on the ATMega32 baudrate, I was reminded of a problem I'm having....

Using the most excellent latest version of WinAVR...

I'm cleaning up my code in proper .h and .c files and want to compile lines based on #define values (specifically for uart frame setting) something like this:

#define frm '8N1'  // or '7N1' or '7E1'
#if frm=='8N1'
  ... set the bits for 8N1
  #warning "Set for 8N1"
#elif frm=='7N1'
  ... set for 7N1
  #warning "set for 7N1"
#elif frm=='7E1'
  .. set for 7E1
  #warning "Set for 7E1"
#else
 #error "Error - no valid frame set"
#endif

The above example does not work but should be valid according the the GNU CPP (C Pre Processor) docs.

If I change to numerical values it works so...

#define frm 1  // 8N1=1 or 2=7N1 or 3=7E1
#if frm==1
  ... set the bits for 8N1
  #warning "set for 8N1"
#elif frm==2
  ... set for 7N1
  #warning "set for 7N1"
#elif frm==3
  .. set for 7E1
  #warning "set for 7E1"
#else
 #error "Error - no valid frame set"
#endif

Obviously I can get around the problem using the second code, but just curious as to why the first code does not work (no error message either). Should it or is it a bug?

Also ... the #warning messages never come out to the console (output) window in either version!! If I change the #warning to #error they are output. is there a makefile option setting for this? I thought -Wall did that?

Again, not a really big deal, but a little burr under the saddle.

Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My understanding of this comes from:

http://gcc.gnu.org/onlinedocs/cp...

That says "expression is a C expression of integer type"

How would "#if frm='8N1'" or (more likely) "#if frm="8N1"" count as an "integer expression"?

I imagine you could call the three cases 'a', 'b' and 'c' because, like it says, character constants are OK and I guess 'a'=0x61, 'b'=0x62, 'c'=0x63'

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Indeed, I verified that 'a', 'b', 'c' is OK and personally I DO see the #warning output (building from within Studio which uses it's own locally generated Makefile)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you using the latest WinAVR toolchain?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes (WinAVR20070122 and Studio 4.13.528)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define FRM_8N1 1
#define FRM_7N1 2
#define FRM_7E1 3

...
#if FRM_FORMAT == FRM_8N1
#  warning "You are using 8N1 framing."
#elif FRM_FORMAT == FRM_7N1
#  warning "You are using 7N1 framing."
#elif FRM_FORMAT == FRM_7E1
#  warning "You are using 7E1 framing."
#else
#  error "Your frame format is not known."
#endif

avr-gcc ... -DFRM_FORMAT=FRM_8N1 ...

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Jörg... It's like my second code snippet. feels like cheeting though ;-)
Any idea on why the #warning messages don't show up?
The code is getting compiled correctly for all options. Very strange.

Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Where are you building from (command line or within Studio)? If the latter then try the former. It's unlikely you could miss a #warning if it is output to stdout/stderr!

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

They show up correctly for me:

$ avr-gcc -DFRM_FORMAT=FRM_7N1 -c foo.c
foo.c:8:4: warning: #warning "You are using 7N1 framing."
$ avr-gcc -DFRM_FORMAT=FRM_FOO -c foo.c
foo.c:12:4: error: #error "Your frame format is not known."
$ avr-gcc  -c foo.c
foo.c:12:4: error: #error "Your frame format is not known."
$ avr-gcc -DFRM_FORMAT=FRM_8N1 -c foo.c
foo.c:6:4: warning: #warning "You are using 8N1 framing."

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

was using "Make All" from PN.
I'll try from the command line. My makefile has the -Wall option set

Thanks all.

BTW.. I only just dicovered that you could build GCC from within Studio.That's pretty cool
Thanks Cliff!