cc-preprocessoravr-gccatmelstudio

C Preprocessor output int at Build


i want to see output of Preprocessor calculating. Only with strings it works so:

#define XSTR(x) STR(x)
#define STR(x) #x

#define BEGIN "test"

#pragma message ".text= " XSTR(BEGIN)

when i set BEGIN to 32/2 the output is: #pragma message: .text= 32/2.

What can i make to solve this? I don´t won´t solutions like this to search in the .lss file:

uint16_t x = BEGIN;
PORTB = x>>8;
PORTA = x;

Thank you.


Solution

  • What can i make to solve this?

    You have to implement math operations in the preprocessor. For example:

    #define _div_1_over_1  1
    #define _div_2_over_1  2
    #define _div_3_over_1  3
    // etc...
    #define _div_30_over_2  15
    #define _div_31_over_2  15
    #define _div_32_over_2  16
    // etc...
    // etc. for every possible a_over_b combinations, many many 1000 lines
    #define DIV(a, b)  _div_##a##_over_##b
    

    After that, you finally can:

    #define XSTR(x) STR(x)
    #define STR(x) #x
    
    #define BEGIN DIV(32, 2)
    
    #pragma message ".text= " XSTR(BEGIN)
    

    Let's say this is one way of implementing division in preprocessor. For other solutions, see BOOST_PP_DIV and P99_DIV from p99 library.