为什么#pragma 被视为预处理器指令? [英] Why is #pragma considered a preprocessor directive?
问题描述
我知道它以 #
开头,它遵循 预处理指令格式.但是预处理器真的关心它吗?#pragma pack
、#pragma once
和我知道的所有其他指令都由编译器处理.this question 中的评论甚至指出 #pragma
指令在预处理阶段幸存下来.所以我的问题是:
I know it starts with #
, and it follows the preprocessing directives format. But does the preprocessor really care about it? The #pragma pack
, #pragma once
, and all other directives I know, are all proceeded by compilers. A comment in this question even states that #pragma
directive survives the pre-processing stage.
So my questions are:
- 既然
#pragma
是用于编译器的,为什么它被认为是一个预处理指令?仅仅是因为它以#
开头吗? - 预处理器真的能用
#pragma
做一些事情吗?
- Since
#pragma
is for compiler, why is it considered a preprocessing directive? Is it just because it starts with#
? - Does preprocessor really do something with
#pragma
?
推荐答案
为什么 #pragma 被视为预处理器指令?
Why is #pragma considered a preprocessor directive?
因为C标准是这么说的.它在预处理指令一章中指定,C17 6.10.6.除此之外,该标准对于 #pragma
应该做什么故意非常模糊,因为整个目的是做一些特定于编译器的事情.或者,如果无法识别编译指示 - 忽略它.
Because the C standard says so. It is specified in the chapter preprocessing directives, C17 6.10.6. Other than that, the standard is intentionally very vague with what #pragma
should do, since the whole purpose is to do something compiler-specific. Or in case the pragma isn't recognized - ignore it.
没有指定某个编译器如何在内部处理编译指示的内容.
How a certain compiler handles the contents of a pragma internally isn't specified.
某些编译指示显然需要进行预处理,特别是启用/禁用某些编译器行为的编译器行为,例如 #pragma warning ...
等.其中很多必须在预处理期间进行评估,否则编译器将不知道如何编译代码.
Some pragmas obviously need to be pre-processed, notably the kind that enables/disables certain compiler behavior like #pragma warning ...
etc. Lots of them must be evaluated during pre-processing or the compiler won't know how to compile the code.
预处理器真的能用 #pragma 做些什么吗?
Does preprocessor really do something with #pragma?
是的,它在翻译阶段 4 中对其进行评估:执行预处理指令,扩展宏调用,并执行 _Pragma 一元运算符表达式."
Yes, it evaluates it in translation phase 4: "Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator expressions are executed."
请注意,将预处理器与编译器分开主要是一种理论模型.实际上,预处理器和编译器通常彼此紧密集成.
Please note that having a pre-processor separated from the compiler is mostly a theoretical model. In reality the pre-processor and compiler are often rather tightly integrated with each other.
这篇关于为什么#pragma 被视为预处理器指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!