1

I'm coding for a ARM with GCC and need to concatenate (##) a name with the a definition, like this:

#define LCD_E_PORT GPIOC 
...
#define RCC_PORT(x) (RCC_APB2Periph_ ## (x)) // ???
...

so that afterRCC_PORT(LCD_E_PORT) I would get RCC_APB2Periph_GPIOC. It is important to say, that the LCD_E_PORT and the RCC_APB2Periph_GPIOC are NOT strings but some low-level system defined pointers (accessing them processor's memory map).

The point of all this is to have one macro, which handles multiple port definitions.

Is there a solution for this?

Removed parantheses
With parentheses

I'm using arm-none-eabi-gcc.

2
  • What happens if you try #define RCC_PORT(x) RCC_APB2Periph_##x ? Commented Feb 26, 2015 at 11:03
  • For example: RCC_PORT(LCD_E_PORT) will result in RCC_APB2Periph_LCD_E_PORT so it is wrong. I've also tried double definition, I mean: #define RCC_PORT1(x) RCC_PORT(x) but after RCC_PORT1(LCD_E_PORT) I get an error that RCC_APB2_Periph_ and "(" don't give a valid token because, like I said, the GPIOx is a (u32 *) ptr. It is that or it doesn't expand macros, or it expands macros to the final definition... All what I need is the definition in the middle. Commented Feb 26, 2015 at 11:35

1 Answer 1

4

The kind of thing you need required more indirection through the pre-processor:

This (defines.c) :

#define TOKENPASTE( x, y ) x ## y
#define TOKENPASTE2( x, y ) TOKENPASTE( x, y )

#define LCD_E_PORT GPIOC
#define RCC_PORT(x)  TOKENPASTE2( RCC_APB2Periph_, x )

#define STRING(x) #x

int main( int argc, char* argv[] )
{
    RCC_PORT(LCD_E_PORT);
}

results in the expected output through gcc -E defines.c we get:

# 1 "defines.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "defines.c"
# 10 "defines.c"
int main( int argc, char* argv[] )
{
    RCC_APB2Periph_GPIOC;
}

See this SO question for more information.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.