This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Concatenation with ";" in macro does not create a valid token

Hello,

I trying to compile some code that compiles in IAR on CCS. I have most of it working now, but I am getting a pre-processors error on a concatenation. This is the define

#define MCU_IO_INPUP(port, pin, func) st( P##port##SEL &= ~BIT##pin##; P##port##DIR &= ~BIT##pin##; )

This is the line that causes the error: ( I have 100 lines exactly the same causing the same error)

MCU_IO_INPUT(HAL_BOARD_IO_BTN_1_PORT, HAL_BOARD_IO_BTN_1_PIN, MCU_IO_TRISTATE);

I get a compiler error that says the ":" does not create a valid token.

Regards,
/Thomas

  • You do not want the ';' character to be combined with the identifier BIT##pin, etc.  Just leave out the ## operator in front of each ';':

    #define MCU_IO_INPUP(port, pin, func) st( P##port##SEL &= ~BIT##pin ; P##port##DIR &= ~BIT##pin ; )

    You'll probably still have some trouble with st; I don't know what st expands to, but it probably won't be legal C code. If you need to put more than one statement in an expression, use a do-while(0) loop, like so:

    #define MCU_IO_INPUP(port, pin, func) do { P##port##SEL &= ~BIT##pin ; P##port##DIR &= ~BIT##pin ; } while (0)
  • Thanks, that worked.

    Thomas