Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to print out compile-time -calculated constants

I'm doing some microcontroller programming and I have code along these lines:

#define F_CPU 8000000
#define F_ADC (F_CPU / 64.0)
#define T_ADC (1.0/F_ADC)

Is there a way to print out the calculated values of, say T_ADC at compile time? I tried stringifying it

#define STRINGIFY(s) XSTRINGIFY(s)
#define XSTRINGIFY(s) #s
#pragma message ("T_ADC " STRINGIFY(T_ADC)) 

But that just gives the macro-expansion "(1/(8000000/64))", not the actual value.

This being a micro-controller program, it's awkward to do a printf at startup time. I'm using gcc and I'm happy to use any non-standard gcc features if that helps.

like image 673
Fasaxc Avatar asked Oct 23 '25 19:10

Fasaxc


1 Answers

As @mbratch and @freddie said, the computation is made by the compiler, so you can not get the result simply using preprocessor directives. The easiest way that comes to mind right now, is to assign the macro to a global const, and then read the value of the const using a debugger, or opening the binary image of the executable (you can get the address of the constant from the memory map file).

const float temp = T_ADC;

Note that you are forced to specify the C type, and this is an essential step since the result of the macro depends on it.

like image 177
omegatre Avatar answered Oct 26 '25 10:10

omegatre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!