Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to escape a C preprocessor directive?

What I am trying to do is have the C preprocessor output #ifdef, #else, and #endif directives. That is, I would like to somehow "escape" a directive so that the output of the preprocessor includes the directive were the preprocessor to run on the output.

Is it possible to "escape" a CPP directive so that it is outputted by the preprocessor such that the output of an escaped directive would be a preprocessor directive if the CPP output were to be itself preprocessed?

like image 429
Daniel Trebbien Avatar asked Oct 07 '11 13:10

Daniel Trebbien


People also ask

How do you stop Ifndef?

The #ifndef must be ended with the #endif directive of the C Programming Language.

How do you cancel Ifdef?

The #ifdef directive must be closed by an #endif directive.

What is #ifndef and #define in C?

In the C Programming Language, the #ifndef directive allows for conditional compilation. The preprocessor determines if the provided macro does not exist before including the subsequent code in the compilation process.

Which preprocessor command is used to remove a definition?

In the C Programming Language, the #undef directive tells the preprocessor to remove all definitions for the specified macro. A macro can be redefined after it has been removed by the #undef directive.


1 Answers

A slight variant of Marcelo Cantos's answer works for me on GNU cpp 4.4.3:

#define HASH(x) x

...

HASH(#)ifdef __cplusplus
class foo { };
HASH(#)endif
like image 168
Ilmari Karonen Avatar answered Sep 21 '22 12:09

Ilmari Karonen