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?


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.

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

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.

Need Your Help

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.