enumsarmc-preprocessorkeil

Comparing enum / define in keil ARM compiler


I'm facing difficulties to make the following conditional compilation working:

enum {  MIDI_USB_DEV_MCU_IDX, MIDI_WC_BLE_IDX, MIDI_CONN_1_IDX, MIDI_CONN_2_IDX,MIDI_IN_OUT_UARTS_NB 
};

#define MIDI_UART_IDX_TO_USE_SYSEXS      MIDI_WC_BLE_IDX

I use the following code to remind me the above define should not be used in Release mode, but the compiler does not warn on the below test.

#if MIDI_UART_IDX_TO_USE_SYSEXS != MIDI_USB_DEV_MCU_IDX
#warning MIDI_UART_IDX_TO_USE_SYSEXS must be MIDI_USB_DEV_MCU_IDX in RELEASE MODE
#endif

From what I read the enums are treated as Int, using the enum in my source works fine but the conditional compiler test fails. Any help or tip would be appreciated Thanks.


Solution

  • Enumeration constants do not exist in precompilation phase. Your condition expression will expand to

    #if 0 != 0
    

    Only way is to make constant work in #if expressions is to define macro constant.

    #define MIDI_USB_DEV_MCU_IDX (0)
    #define MIDI_WC_BLE_IDX      (1)
    #define MIDI_CONN_1_IDX      (2)
    #define MIDI_CONN_2_IDX      (3)
    #define MIDI_IN_OUT_UARTS_NB (4)
    

    If you are okay with compilation failing on error, then you can use enumeration constants with static assertion:

    _Static_assert(
        IDI_UART_IDX_TO_USE_SYSEXS == MIDI_USB_DEV_MCU_IDX, 
        "MIDI_UART_IDX_TO_USE_SYSEXS must be MIDI_USB_DEV_MCU_IDX in RELEASE MODE");