pragma 指令的范围是什么?

发布于 2024-10-18 08:13:22 字数 134 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷离° 2024-10-25 08:13:22

直到翻译单元结束。通俗地说,TU 是源文件及其包含文件。

通常的模式是这样的:

#pragma warning (push) //save
#pragma warning (disable: xxxx)
#pragma warning (disable: yyyy)
...

//code

#pragma warning (pop) //restore prev settings

例如

//A.h
#pragma once
#pragma warning (disable: 1234)
#include "b.h"

//b.h
#pragma once
//when included after a.h 1234 will be disabled

//c.cpp
#include "a.h" //warnings 1234 from b.h is disabled

//d.cpp
#include "b.h" //warnings 1234 from b.h are not disabled
#include "a.h"

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:

#pragma warning (push) //save
#pragma warning (disable: xxxx)
#pragma warning (disable: yyyy)
...

//code

#pragma warning (pop) //restore prev settings

for example

//A.h
#pragma once
#pragma warning (disable: 1234)
#include "b.h"

//b.h
#pragma once
//when included after a.h 1234 will be disabled

//c.cpp
#include "a.h" //warnings 1234 from b.h is disabled

//d.cpp
#include "b.h" //warnings 1234 from b.h are not disabled
#include "a.h"
温柔戏命师 2024-10-25 08:13:22

编译指示特定于所使用的编译器和平台。
所以最好的办法是查看编译器的文档。

对于 IBM 编译器,例如:

许多 pragma 指令可以在任意位置指定
源代码中的点
编制单位;其他人必须是
在任何其他指令之前指定
或源代码语句。在
每个的单独描述
pragma,“用法”部分描述了
对编译指示的任何限制
放置。

一般来说,如果您指定一个编译指示
指令之前的任何代码
源程序,它适用于
整个编译单元,包括任何
包含的头文件。对于一个
可以出现在任何位置的指令
你的源代码,它适用于
指定的点,直到
编译单元结束。

您可以进一步限制范围
编译指示的应用程序通过使用
互补的编译指示对
围绕选定部分的指令
的代码。例如,使用#pragma
选项源和#pragma 选项
nosource 指令如下
要求仅选择选定的部分
您的源代码包含在
您的编译器列表:

#pragma 选项源 

/* source 和 nosource 编译指示之间的源代码
    选项包含在编译器列表中*/

#pragma 选项 nosource

许多编译指示提供“pop”或“reset”
允许您启用的子选项
并禁用 a 中的 pragma 设置
基于堆栈的时尚;这些的例子
在相关的编译指示中提供
描述。

一般来说,编译指示应该在声明后立即生效,无论它来自哪个标头,直到翻译单元结束。然而,有一些编译指示会影响整个程序。例如,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:

Many pragma directives can be specified at any
point within the source code in a
compilation unit; others must be
specified before any other directives
or source code statements. In the
individual descriptions for each
pragma, the "Usage" section describes
any constraints on the pragma's
placement.

In general, if you specify a pragma
directive before any code in your
source program, it applies to the
entire compilation unit, including any
header files that are included. For a
directive that can appear anywhere in
your source code, it applies from the
point at which it is specified, until
the end of the compilation unit.

You can further restrict the scope of
a pragma's application by using
complementary pairs of pragma
directives around a selected section
of code. For example, using #pragma
options source and #pragma options
nosource directives as follows
requests that only the selected parts
of your source code be included in
your compiler listing:

#pragma options source 

/*  Source code between the source and nosource pragma
    options is included in the compiler listing                */

#pragma options nosource

Many pragmas provide "pop" or "reset"
suboptions that allow you to enable
and disable pragma settings in a
stack-based fashion; examples of these
are provided in the relevant pragma
descriptions.

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".

揽月 2024-10-25 08:13:22

是的,它还会禁用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文