Possible Duplicate:
C preprocessor: using #if inside #define?
Is there any trick to have preprocessor directives inside rhs of define ? The problem is, preprocessor folds all rhs into one long line. But maybe there is a trick ?
Example of what I’d want in the rhs is
#define MY_CHECK \
#ifndef MY_DEF \
# error MY_DEF not defined \
#endif
?
The purpose is a shortness: to have 1-line shortcut instead of multiline sequence of checks.
As others have noted, preprocessor macros cannot expand into any other preprocessor directives; if they do you’ll generally get odd errors about stray ‘#’ characters in the input. However, sometimes there are things you can do to get what you want. If you want a macro that expands to something like:
where some code doesn’t include any preprocessor directives, you can define an IFDEF macro:
and then use
IFDEF_SOMETHING(...some code...)in your other macro.If you have a bunch of preprocessor cruft that you want to repeat multiple times, you can stick it in its own file and then use
#include "stuff"in each spot you need it.