views:

957

answers:

6

OK, without going into the gory details I want to use a #define macro that will expand to a #include but the '#' sign is confusing the preprocessor (as it thinks I want to quote an argument.)

For example, I want to do something like this:

#define MACRO(name) #include "name##foo"

And use it thus:

MACRO(Test)

Which will expand to:

#include "Testfoo"

The humble # sign is causing the preprocessor to barf. MinGW gives me the following error:

'#' is not followed by a macro parameter

I guess I need to escape the # sign but I don't if this is even possible.

Yes, macros are indeed evil...

+1  A: 

This might work (it works for regular #define macros with no parameters, but I haven't tested it with macros with parameters).

#define MACRO(name) <name##foo>
#include MACRO(Test)
280Z28
Use `<>` instead of `""`. `""` Are not interpolated.
EFraim
that will not allow for the case when you want to replace it with two #includes, or perhaps eliminate them altogether. But it's perhaps the best option.
Marius
+2  A: 

As far as I remember you cannot use another preprocessor directive in define.

EFraim
Thanks. I found a better way to achieve what I was after in the end, without using macros.
Rob
It would have been nice if you mentioned in which way you achieved what you wanted? :-)
pbean
+1  A: 

This is because the # has special meaning when used in a macro.

#  means quote the following token (which should be a macro parameter name)
## means concatenate the preceding and following tokens.

In your situation the # is not followed by a proper token. So in your situation we need to go through a level of indiection:

#define     QUOTE(name)     #name
#define     TEST(name)      QUOTE(name ## foo)

#include TEST(scot)
Martin York
A: 

This seems to work:

$ cat test.c
#define INC #include
#define MACRO(name) INC <name##foo>
MACRO(hello)

using cpp (the preprocessor) on this results in :

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


#include <hellofoo>

Edit:

Hmmm... I do not think this actually works. cpp is fine with it but gcc is not... Actually it seems that cpp generates the correct output but then it does not see the #include and does not include the file. So maybe it works if you first run cpp manually on your source and then use gcc on the sources cpp creates.

rve
as stated above, the preprocessor does not re-parse the file. indeed to get a # into it may be as simple as a single quote : \#
Marius
+5  A: 

The problem isn't actually getting a # symbol in the output of your preprocessor.

Apparently you want the preprocessor to reparse your file, to deal with newly created #include directives as part of macro expansion. It doesn't work that way. If a line starts with #, it's an instruction for the preprocessor and interpreted. If a line doesn't start with #, it's only subject to preprocessor transformation including macro substitution. This is a once-per-line test.

MACRO(Test)

does not start with #. Therefore it is not interpreted as a preprocessor directive; instead it's subject to macro replacement rules.

MSalters
Clearest answer on here I think - effectively he's asking for a 2nd parse of the preprocessor, well noted.
polyglot
A: 

You can't do that. Preprocessor directives are recognized before macro expansion; if the macro expands into something that looks like a preprocessor directive, that directive will not be recognized. The best you can do is create a macro for the file name:

#define MACRO(name) "name##foo"
...
#include MACRO(Test)
John Bode