cancel
Showing results for 
Search instead for 
Did you mean: 

Get calculated values from defined constants in macro

Nickelgrass
Senior II

Hello,

I am using STM32CubeIDE (eclipse) and was wondering if it is possible to get the constant calculated value of a macro.

For example

#define A 1
#define B 2
#define R A+B   // R is now 3

#if R > 2
  #warning R is greater 
#else
  #warning R is less 2
#endif

I can only see 1+2 in the macroexpansion. But clearly the IDE has calculated the value and can evaluate the #if ... #else and gray out the unused part. So how can i access this calculated value? 

12 REPLIES 12

@Ozone wrote:

the preprocessor ... uses different datatypes and ranges than the compiler


@Nickelgrass  more details here.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
Nickelgrass
Senior II

Thanks for all the replies. I solved it by simply putting a little python script in the prebuild step that parses and calculates the values and displays them in the console. Would be nice though to have such a functionality build in. Case closed. 


@Nickelgrass wrote:

Case closed. 


To do that, please mark the solution on the post which provided the answer - not this one!

 

BTW:


I wrote:

Or write some test code that will do it for you ...


The Compiler Explorer could be useful for that - you can copy & paste the full macro expansion from the IDE into it.

 

I had a quick google to see if there's a site where you can just paste-in a C expression and have it evaluated - didn't find one.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.