This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Macro expansion and the preprocessor


On 14 January 2011 17:55, Adam Mercer wrote:
>
> So it seems that the expansions aren't be done as I would expect them to be.
>
> Can anyone see what I'm doing wrong?

If you use the -E option to gcc it will print the preprocessor output,
so you can see what it's doing.

Life will be much easier if you keep your example much simpler, until
you get the macros working e.g.

$ cat test.c
#define TYPE COMPLEX16
#define GTYPE TYPE##Grid
GTYPE g;
#undef TYPE

Now you can see what's happening

$ gcc -E test.c
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"


TYPEGrid g;


The problem is that the ## operator doesn't expand its arguments, so
TYPE is used verbatim.

You need to add some levels of indirection, so that GTYPE expands to a
macro which expands TYPE, and passes that to another macro which does
the concatenation.

$ cat test.c
#define TYPE COMPLEX16
#define DO_JOIN2(a, b) a##b
#define JOIN2(a, b) DO_JOIN2(a, b)
#define GTYPE JOIN2(TYPE, Grid)
GTYPE g;
#undef TYPE


$ gcc -E test.c
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"




COMPLEX16Grid g;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]