pragma 指令的范围是什么?
pragma 指令的范围是什么?例如,如果我在另一个文件 B 包含的头文件 A 中说 #pragma warning(disable: 4996)
,是否也会禁用 B 内的所有这些警告?或者我应该再次启用文件 A 末尾的警告吗?
What is the scope of a pragma directive? For example, if I say #pragma warning(disable: 4996)
in a header file A that is included from a different file B, will that also disable all those warnings inside B? Or should I enable the warning at the end of file A again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
直到翻译单元结束。通俗地说,TU 是源文件及其包含文件。
通常的模式是这样的:
例如
It is till the end of the translation unit. Informally, a TU is the source file with its include files.
The usual pattern is this:
for example
编译指示特定于所使用的编译器和平台。
所以最好的办法是查看编译器的文档。
对于 IBM 编译器,例如:
一般来说,编译指示应该在声明后立即生效,无论它来自哪个标头,直到翻译单元结束。然而,有一些编译指示会影响整个程序。例如,Microsoft 特定的“链接”编译指示将某些库的依赖项添加到翻译单元及其所有“用户”。
Pragmas are specific for the compiler and platform in use.
So the best bet is to look at compiler's documentation.
For IBM compilers, for example:
Generally, pragma should have effect right after its declaration, no matter from what header it comes, until the end of translation unit. However, there are some pragmas that affect the whole program. For example, Microsoft-specific "link" pragma that adds dependency on some library to the translation unit and all its "users".
是的,它还会禁用 B 内的警告。
翻译单元是一个 .cpp 文件,其所有包含的文件都会扩展为一个大文件。该编译指示将持续到翻译单元的末尾,或者直到另一个 #pragma 警告更改设置。或者,如果您的编译器支持#pragma push 和#pragma pop,它将持续到下一个#pragma pop。
'#pragma push' 和 '#pragma pop' 允许您创建范围。此类范围内的#pragma 警告将应用于该范围的末尾。
Yes, it will also disable the warnings inside B.
A translation unit is a .cpp file and all its included files expanded out into one great big file. That pragma will last to the end of the translation unit, or until another #pragma warning changes the setting. Or, if you're compiler supports #pragma push and #pragma pop, it will last until the next #pragma pop.
'#pragma push' and '#pragma pop' allow you to create scopes. #pragma warnings within such a scope will apply to the end of the scope.