Hello,
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
Hello,
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
Why does the spacing matter?
The imake program used in (older?) builds of X11 used the C pre-processor to generate makefiles, but imake program used a special technique of indicating line endings with @@ symbols at the ends of lines, and then post-processed the output of the pre-processor to replace the @@ symbols with newlines.
From this design, I conclude that there is no reliable way to obtain newlines from expanded macros in C (or C++). Indeed, for C, there is no need since:
I'm pretty sure CPP, being designed for C which doesn't care for newlines, and all, can't handle this kind of work. Still you can mark wanted newlines with some special marker string and pass the result through sed
or awk
to get what you want.
I have the same need. One very good reason for wanting macros to expand into multiple lines is to 'produce' source code from macros that can be debugged. Who likes single stepping 30 times in a macro without seeing new statements for each step ? So if gcc supported the AT&T research cpp extension '#macdef' and '#endmac' to define multiline macros with having to use line continuation characters (thus in practise producing long single line statements) would be great !!!!!
I have also played around with trying to trick gcc cpp to produce newline in the expansion with no sucess :-(
Got it!
#define anlb /*
*/ A /*
*/ B
anlb anlb
V
gcc -E -CC nl.c
V
/*
*/ A /*
*/ B /*
*/ A /*
*/ B
What he wants is a way to make the pre-processor to add a new line
This code for example:
struct C_##typ {\
typ d_##typ; \
};\
\
typedef struct C_##typ D_##typ;\
typ* D_##typ##_new(){\
return (typ*) malloc(sizeof(typ));\
};\
The pre-processor would put all in one line:
struct C_MyClass { MyClass d_MyClass; };typedef struct C_MyClass D_MyClass;MyClass* D_MyClass_new(){ return (MyClass*) malloc(sizeof(MyClass));};
and he wants:
struct C_MyClass {
MyClass d_MyClass;
};
typedef struct C_MyClass D_MyClass;
MyClass* D_MyClass_new(){
return (MyClass*) malloc(sizeof(MyClass));
};
Is there or not a way to force the preprocessor to do that?