Go Down

Topic: output preprocessor variables (Read 6363 times) previous topic - next topic

wally123

Hello,

how can i get the values hold in preprocessor variables ?
e.g.:
Code: [Select]
#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
  #include "SoftwareSerial.h"
  SoftwareSerial port(12,13);
#else // Arduino 0022 - use modified NewSoftSerial
  #include "WProgram.h"
  #include "NewSoftSerial.h"
  NewSoftSerial port(12,13);
#endif


i want to output the content of "ARDUINO" during compilation

wally


AWOL

What do you mean by "output during compilation"?

wally123

output as a Warning or whatever in the console area of the IDE when hit 'verify'.
I just want to see the variable contents during preprocessor runs.

PaulS

Quote
output as a Warning or whatever in the console area of the IDE when hit 'verify'.

When you rewrite the preprocessor, you'll be able to do that.

PeterH

In this particular example the ARDUINO preprocessor definition is made by a command-line argument on the avr-gcc command line that invokes the compiler, which you can see in the message area if you enable verbose output during compilation.

There is no general way to display the value of a preprocessor definition - you can use #error or #warning preprocessor directives to output messages at compilation time but those directives do not support macro expansion in the message that is output. If you want to see what value a given definition has, you could put code in to print it at run time.

nickgammon

#5
Sep 22, 2013, 12:45 am Last Edit: Sep 22, 2013, 12:48 am by Nick Gammon Reason: 1
1. Turn on verbose compiling.

2. Do a compile.

3. Copy the command for compiling your main sketch, eg.
[font=Courier]
/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/cores/arduino -I/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/variants/standard /var/folders/1l/43x8v10s1v36trvjz3v92m900000gn/T/build3186491510186073955.tmp/sketch_sep21b.cpp -o /var/folders/1l/43x8v10s1v36trvjz3v92m900000gn/T/build3186491510186073955.tmp/sketch_sep21b.cpp.o[/font]

4. Change the -c part to -dM -E

5. Get rid of the -o ... part (the output).

Results should look like this on a Mac (YMMV):

[font=Courier] /Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -dM -E -I/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/cores/arduino -I/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/variants/standard /var/folders/1l/43x8v10s1v36trvjz3v92m900000gn/T/build3186491510186073955.tmp/sketch_sep21b.cpp [/font]

Execute the above command in a "terminal" or command window. You then see your defines, eg.

Code: [Select]

#define __LPM_classic__(addr) (__extension__({ uint16_t __addr16 = (uint16_t)(addr); uint8_t __result; __asm__ ( "lpm" "\n\t" "mov %0, r0" "\n\t" : "=r" (__result) : "z" (__addr16) : "r0" ); __result; }))
#define INT0 0
#define INT1 1
#define __DBL_MIN_EXP__ (-125)
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define B00010000 16
#define B11101000 232
#define B11101001 233
#define PRADC 0
#define Arduino_h
#define sq(x) ((x)*(x))
#define B01000100 68
#define B01000101 69
#define __LPM_dword_classic__(addr) (__extension__({ uint16_t __addr16 = (uint16_t)(addr); uint32_t __result; __asm__ ( "lpm" "\n\t" "mov %A0, r0" "\n\t" "adiw r30, 1" "\n\t" "lpm" "\n\t" "mov %B0, r0" "\n\t" "adiw r30, 1" "\n\t" "lpm" "\n\t" "mov %C0, r0" "\n\t" "adiw r30, 1" "\n\t" "lpm" "\n\t" "mov %D0, r0" "\n\t" : "=r" (__result), "=z" (__addr16) : "1" (__addr16) : "r0" ); __result; }))
#define B11101010 234
#define OCF0A 1
#define OCF0B 2
#define fdev_set_udata(stream,u) do { (stream)->udata = u; } while(0)
#define B01000110 70
#define B01000111 71
#define noInterrupts() cli()
#define OCF1A 1
#define OCF1B 2
#define __FLT_MIN__ 1.17549435e-38F
...


There's a lot of them (I got 1,693 lines).
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon

More simply ...

1. Grab the compile command, eg.

[font=Courier]/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/cores/arduino -I/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/variants/standard /var/folders/1l/43x8v10s1v36trvjz3v92m900000gn/T/build3186491510186073955.tmp/sketch_sep21b.cpp -o /var/folders/1l/43x8v10s1v36trvjz3v92m900000gn/T/build3186491510186073955.tmp/sketch_sep21b.cpp.o [/font]

2. Paste into a console/terminal/command window and then backspace to omit the last bit highlighted above (the -o onwards).

3. At the end of the line append "-dM -E".

4. Now you have:

[font=Courier]/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/cores/arduino -I/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/variants/standard /var/folders/1l/43x8v10s1v36trvjz3v92m900000gn/T/build3186491510186073955.tmp/sketch_sep21b.cpp -dM -E [/font]

5. Execute that.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon


how can i get the values hold in preprocessor variables ?


Why do you want to?
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

TanHadron

I have wanted to do this at times.  I found out the same thing PeterH brought up with the #warning and #error processor directives -- it doesn't expand the values out.  I have actually written small sketches for the purpose of printing out #define values at run time that the compiler should have been able to tell me at compile time.

Thanks, Nick.  That is a minor pain to do, and there are a ton of defines to go through, but that worked for me.

nickgammon

Yeah, you can redirect into a file, and then edit that. Or pipe it though grep, eg.

Code: [Select]

/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ ... -dM -E  | grep whatever
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

michael_x

#10
Sep 24, 2013, 08:19 am Last Edit: Sep 24, 2013, 08:25 am by michael_x Reason: 1
or simply look at what Nick provided:
Quote
3. Copy the command for compiling your main sketch, eg.

/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/cores/arduino -I/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/arduino/variants/standard /var/folders/1l/43x8v10s1v36trvjz3v92m900000gn/T/build3186491510186073955.tmp/sketch_sep21b.cpp -o /var/folders/1l/43x8v10s1v36trvjz3v92m900000gn/T/build3186491510186073955.tmp/sketch_sep21b.cpp.o


( compare that with your output)
I see that Nick is runnung Arduino 1.05 , so his compiler is called to acts as if there were a line
#define ARDUINO 105
before the first line of code.

Edit:
But I understand your underlying question is , how to get a preprocessor definintion into a #warning directive ?

westfw

I watched a video recently that implied that you should be able to do this with "C++ template metaprograming" or something like that.  The example had the compiler spitting out digits of pi (as error messages.)

http://vimeo.com/44792649

Sorry. I'm AFK, and not able to pinpoint the locate the exact spot.  It's all rather interesting...

Tom Carpenter

Did you put this code at the top of your sketch? or was it in a library.

If it was at the top of the sketch, there is a glitch in the IDE where it completely ignores the #if statement.
~Tom~

PeterH


Sorry. I'm AFK, and not able to pinpoint the locate the exact spot.  It's all rather interesting...


The relevant part starts about 29 minutes in. What a fascinating presentation!

Go Up