5

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.

Emre İriş
  • 77
  • 1
  • 5
  • 1
    Because #pragma GCC optimize(...) can change optimization level. – Emre İriş Feb 24 '21 at 19:04
  • Does this answer your question? [Use of #pragma in C](https://stackoverflow.com/questions/232785/use-of-pragma-in-c) – Antonin GAVREL Feb 24 '21 at 19:06
  • Pragmas are listed as preprocessor directives, but used to control the compiler: https://gcc.gnu.org/onlinedocs/cpp/Pragmas.html – Eugene Sh. Feb 24 '21 at 19:06
  • I edited the question to clarify. – Emre İriş Feb 24 '21 at 19:13
  • 1
    Your last paragraph is specifically addressed in the link above - about introducing the `_Pragma` operator, which is *not* a preprocessor directive. – Eugene Sh. Feb 24 '21 at 19:14
  • @EugeneSh. It's not an actual operator either. GCC and Clang preprocessors replace `_Pragma` with `#pragma`. – HolyBlackCat Feb 24 '21 at 19:18
  • gcc is not really doing preprocessing as a separate pass unless you ask it to, see [-no-integrated-cpp](https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html) and even if you do, `#pragma` directives survive preprocessing intact and are still seen by the compiler (try it by running your source through `cpp`). –  Feb 24 '21 at 19:19
  • @dratenik they have to survive as they control compilation and code generation – 0___________ Feb 24 '21 at 19:21
  • @HolyBlackCat Well, they call it an operator. Also it can be emitted as a macro expansion unlike the `#pragma` directive – Eugene Sh. Feb 24 '21 at 19:23

2 Answers2

3

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.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • So preprocess may leave command to control compiler process. But it highly depends on the compiler. Thanks for your valuable answer. – Emre İriş Feb 24 '21 at 19:30
1

#pragmas are compiler extensions and their behaviour is defined by the compiler. They start with # like preprocessor directives, but they are not one of them. Some compilers have other extensions like gcc __attribute__ which can also set how the compiler compiles the source code and generates the output.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    Pragmas in general are C standard: http://port70.net/~nsz/c/c11/n1570.html#6.10.6 Specific pragmas might be an extension. – Eugene Sh. Feb 24 '21 at 19:24