1

I was just wondering whether the C++ preprocessor is capable of macros such as:

#define include<a> include<a.h>

Which would convert

#include<stdio>

into

#include<stdio.h>

Does anyone have any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Forrest4096
  • 159
  • 1
  • 8
  • The accepted answer to the duplicate question isn't all that helpful. A number of the other answers are more helpful. The key point is that even if the output of macro processing looks like a preprocessor directive, it is _not_ a preprocessor directive. So you can't generate the `#include` part of the line from a macro. You can, however, generate the file name part of the `#include` line as a macro. You just have to be a bit careful, that's all. – Jonathan Leffler Dec 27 '14 at 09:01

1 Answers1

3

The #include directives can't be replaced directly with a macro. However, the entity to be included can be the result of a macro expansion. That is, if you need to use different header names, you can define a macro which expands to what is being included, e.g.:

#define CONCAT(a,b) a ## b
#ifdef USE_C_NAMES
#   define MAKE_NAME(x) <x.h>
#else
#    define MAKE_NAME(x) <CONCAT(c,x)>
#endif
#include MAKE_NAME(stdio)
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380