Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preprocessor directives inside define? [duplicate]

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.

like image 804
Andrei Avatar asked May 27 '11 05:05

Andrei


1 Answers

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:

#ifdef SOMETHING
...some code...
#endif

where some code doesn't include any preprocessor directives, you can define an IFDEF macro:

#ifdef SOMETHING
#define IFDEF_SOMETHING(X) X
#else
#define IFDEF_SOMETHING(X)
#endif

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.

like image 75
Chris Dodd Avatar answered Oct 07 '22 08:10

Chris Dodd