cc-preprocessor

C #pragma is preprocess or compile time operation?


I have been working on GCC pragma types and it's operations. But I realized that pragmas can be used to command directly compiler. The confusion I am having is that # operations are part of preprocessing, for example

#if DEBUG
 /* statement one */
#elif RELEASE
 /* statement two */
#endif

if debug mode is activated, the compiler does not even compile and detect errors in statement two, but how does #pragma can directly command to the compiler?

Also If It is controlling the compiler, is there a way to do it without #pragmas? Because after preprocessing there is only C code left.


Solution

  • is there a way to do it without #pragmas?

    No, as far as I know.

    Because after preprocessing there is only C code left.

    Turns out, at least GCC and Clang don't remove #pragma during preprocessing, and leave it for the compiler to handle. They also appear to replace _Pragma with #pragma.

    You can see it by invoking them with the -E flag to output the preprocessed source.

    pragmas can be used to command directly compiler. The confusion I am having is that # operations are part of preprocessing

    Yes, C++ is weird. The standard calls it a 'preprocessor directive', but doesn't define what it can or can't do. GCC developers decided to let it affect the compiler too.