preprocessor directives inside define? [duplicate]

早过忘川 提交于 2019-12-04 01:59:01

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.

It won't work (§6.10.3.4/3: "The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one ...").

If you really want to do things like this, you can run your source through something like m4 before compilation -- but I'd generally recommend against it.

Assuming a preprocessor like the GNU C Preprocessor, then no. The manual says:

The compiler does not re-tokenize the preprocessor's output. Each preprocessing token becomes one compiler token.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!