Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make G++ preprocessor output a newline in a macro?

Is there a way in gcc/g++ 4.* to write a macro that expands into several lines?

The following code:

#define A X \ Y 

Expands into

X Y 

I need a macro expanding into

X Y 
like image 869
Michael Rybak Avatar asked Feb 16 '10 06:02

Michael Rybak


People also ask

What is __ LINE __ in C?

__LINE__ is a preprocessor macro that expands to current line number in the source file, as an integer. __LINE__ is useful when generating log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.

What does ## mean in C macro?

The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token, and therefore, can't be the first or last token in the macro definition.

What is macro expansion in preprocessor?

Advertisements. When the program run and if the C preprocessor sees an instance of a macro within the program code, it will do the macro expansion. It replaces the macro template with the value of macro expansion. The #define directive is a good way of declaring constant compared to a constant or a variable value.

Which of the following lines is the end of a macro?

The \ at the end of each line of the macro definition is used to allow the macro definition to be split into multiple physical source lines (presumably for readability reasons). This works because a \ followed by a new-line is deleted during phase two of translation, but the preprocessor is run later, in phase four.


2 Answers

Got it!

#define anlb /* */ A /* */ B  anlb anlb 

Down arrow

gcc -E -CC nl.c 

Down arrow

/* */ A /* */ B /* */ A /* */ B 
like image 70
Potatoswatter Avatar answered Sep 29 '22 07:09

Potatoswatter


Make the macro generate a special markup, say __CR__, then pipe the result of CPP into a script which translates the macro to a true newline, for example, sed 's/__CR__/\n/g'.

I just found this useful to generate a code pattern to be filled by hand. It is quite easier when the code is readable.

like image 25
Eric Dujardin Avatar answered Sep 29 '22 07:09

Eric Dujardin