Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessor macros substitution

#define LED1_ON() { /* twiddle port bit */ }
#define LED2_ON() { /* twiddle port bit */ }
//  ...
#define LED9_ON() { /* twiddle port bit */ }

#define LED_ON(x) LED##x_ON()

I would like to use the above code (or something similar) to have (for example) LED_ON(1) call macro LED1_ON(), or LED_ON(2) call macro LED2_ON().

I believe it is possible to make cpp do this, but clearly I don't have the syntax correct. Does anyone know the syntax to make this possible?

On the line where I call the LED_ON(2), gcc gives the error message:

undefined reference to `LEDx_ON'
like image 819
loneRanger Avatar asked Dec 04 '22 08:12

loneRanger


1 Answers

You need to define LED_ON as

#define LED_ON(x)  LED##x##_ON()

you want to take the argument x and paste on an LED prefix and a _ON suffix.

like image 134
Chris Dodd Avatar answered Dec 26 '22 14:12

Chris Dodd