简体   繁体   中英

How to distinguish preprocessor and compiler directives?

I have been told that #pragma omp directive in GCC is directive of the compiler, and is not directive of the preprocessor.

Is it correct?

How to distinguish preprocessor's and compiler's directives?

gcc -E runs the preprocessor only. So inspect the output of that: anything left in there is for the attention of the compiler proper.

With some experience of C++ you won't need to do this every time because you'll learn what the preprocessor does and what the compiler does. Some of the things controlled by #pragma cannot conceivably be done by the preprocessor, so it follows that it must be a compiler directive in those cases (or in theory it could be replaced with an equivalent compiler directive by the preprocessor -- if you care about the difference then again, gcc -E will show what happens). However, some of the things #pragma does relate to preprocessing ( #pragma once ), and so it must be a preprocessor directive in those cases.

Your example #pragma omp is a compiler directive by both tests. By general knowledge, the preprocessor isn't anything like smart enough to parallelize code. It doesn't even understand most of the C++ code it sees, basically all it can do is integer arithmetic with constants, macro replacement, and shovelling text around. For a direct test with gcc -E try the following file:

#if 1
    #pragma omp
#endif

output is some filename/line-number annotations plus:

#pragma omp

So we observe that #if and #endif have been handled by the preprocessor, while #pragma omp has not.

Here is a quote from gcc docs

This manual documents the pragmas which are meaningful to the preprocessor itself. Other pragmas are meaningful to the C or C++ compilers. They are documented in the GCC manual.

Acccording to that, there are preprocessor pragmas and non-preprocessor pragmas.

How to distinguish preprocessor's and compiler's directives?

Preprocessor directives are specified in the C standard, compiler directives are described in the compiler manual.

About your edit, the linked page does not mention #pragma omp and if you combine that with the quote above, I would reason that the pragma is not for the preprocessor. It is definitely compiler specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM