Is there a way to get the C preprocessor (GCC) to run two passes, to fully expand macros?
I'm trying to define macros that support port I/O on a microcontroller using abstract names for pins:
#define LED_RED E, (1<<6)
#define SetPin(port, mask) PORT ## port ## SET = (mask)
#define ClearPin(port, mask) PORT ## port ## CLR = (mask)
#define ReadPin(port, mask) (PORT ## port) & (mask)
Then in my code:
SetPin(LED_RED);
ClearPin(LED_RED);
result = ReadPin(LED_RED);
This is meant to expand to:
PORTESET = (1<<6);
PORTECLR = (1<<6);
result = (PORTE) & (1<<6);
This doesn't compile - I get:
error: macro "SetPin" requires 2 arguments, but only 1 given.
Although this does compile and work OK:
SetPin(E, (1<<6));
So...how to get the C compiler to fully expand these macros?
Or, if not that, how to make this kind of thing work?
Preprocessing is the first step of the language processing system. Language processing system translates the high level language to machine level language or absolute machine code(i.e. to the form that can be understood by machine).
There are 4 Main Types of Preprocessor Directives: Macros. File Inclusion. Conditional Compilation. Other directives.
The C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation. We'll refer to the C Preprocessor as CPP.
The preprocessor still recognizes and removes comments, so that you can pass a file preprocessed with -C to the compiler without problems.
You have to pass the arguments through an additional macro.
#define LED_RED E, (1<<6)
#define SetPin2(port, mask) PORT ## port ## SET = (mask)
#define SetPin(x) SetPin2(x)
SetPin(LED_RED);
This is due to the order of macro replacement:
##
or #
, they get macro-expanded
before.With the additional macro "in between", the 2nd steps gets the chance to replace LED_RED
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With