Hello,
how can i get the values hold in preprocessor variables ?
e.g.:
#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
system
September 21, 2013, 6:54am
2
What do you mean by "output during compilation"?
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.
system
September 21, 2013, 12:12pm
4
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.
system
September 21, 2013, 4:26pm
5
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.
Turn on verbose compiling.
Do a compile.
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
Change the -c part to -dM -E
Get rid of the -o ... part (the output).
Results should look like this on a Mac (YMMV):
/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
Execute the above command in a "terminal" or command window. You then see your defines, eg.
#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).
More simply ...
Grab the compile command, 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
Paste into a console/terminal/command window and then backspace to omit the last bit highlighted above (the -o onwards).
At the end of the line append "-dM -E".
Now you have:
/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
Execute that.
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.
Yeah, you can redirect into a file, and then edit that. Or pipe it though grep, eg.
/Applications/Arduino_1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ ... -dM -E | grep whatever
or simply look at what Nick provided:
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
September 24, 2013, 6:39am
12
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...
TCWORLD
September 24, 2013, 8:37am
13
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.
system
September 25, 2013, 4:48pm
14
westfw:
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!