pragma指令的范围是什么?

时间:2022-07-30 13:36:24

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?

pragma指令的范围是什么?例如,如果我在一个包含在不同文件B中的头文件A中说#pragma warning(disable:4996),那还会禁用B内的所有警告吗?或者我应该再次在文件A的末尾启用警告?

3 个解决方案

#1


17  

It is till the end of the translation unit. Informally, a TU is the source file with its include files.

直到翻译单元结束。非正式地,TU是包含其文件的源文件。

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"

#2


8  

Pragmas are specific for the compiler and platform in use. So the best bet is to look at compiler's documentation.

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

For IBM compilers, for example:

对于IBM编译器,例如:

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.

可以在编译单元的源代码中的任何点指定许多pragma伪指令;必须在任何其他指令或源代码语句之前指定其他语句。在每个pragma的各个描述中,“Usage”部分描述了对pragma放置的任何约束。

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.

通常,如果在源程序中的任何代码之前指定pragma伪指令,它将应用于整个编译单元,包括所包含的任何头文件。对于可以出现在源代码中的任何位置的指令,它从指定的位置开始应用,直到编译单元结束。

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指令对来进一步限制pragma应用程序的范围。例如,如下所示,使用#pragma options source和#pragma options nosource指令请求只将源代码的选定部分包含在编译器列表中:

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

一般来说,pragma在声明之后应该有效,无论它来自哪个标题,直到翻译单元结束。但是,有一些pragma会影响整个程序。例如,特定于Microsoft的“链接”编译指示,它将某些库的依赖性添加到翻译单元及其所有“用户”。

#3


1  

Yes, it will also disable the warnings inside B.

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

翻译单元是.cpp文件,其所有包含的文件扩展为一个很棒的大文件。该pragma将持续到翻译单元的末尾,或者直到另一个#pragma警告更改设置。或者,如果您的编译器支持#pragma push和#pragma pop,它将持续到下一个#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.

'#pragma push'和'#pragma pop'允许您创建范围。此范围内的#pragma警告将应用于范围的末尾。

#1


17  

It is till the end of the translation unit. Informally, a TU is the source file with its include files.

直到翻译单元结束。非正式地,TU是包含其文件的源文件。

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"

#2


8  

Pragmas are specific for the compiler and platform in use. So the best bet is to look at compiler's documentation.

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

For IBM compilers, for example:

对于IBM编译器,例如:

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.

可以在编译单元的源代码中的任何点指定许多pragma伪指令;必须在任何其他指令或源代码语句之前指定其他语句。在每个pragma的各个描述中,“Usage”部分描述了对pragma放置的任何约束。

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.

通常,如果在源程序中的任何代码之前指定pragma伪指令,它将应用于整个编译单元,包括所包含的任何头文件。对于可以出现在源代码中的任何位置的指令,它从指定的位置开始应用,直到编译单元结束。

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指令对来进一步限制pragma应用程序的范围。例如,如下所示,使用#pragma options source和#pragma options nosource指令请求只将源代码的选定部分包含在编译器列表中:

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

一般来说,pragma在声明之后应该有效,无论它来自哪个标题,直到翻译单元结束。但是,有一些pragma会影响整个程序。例如,特定于Microsoft的“链接”编译指示,它将某些库的依赖性添加到翻译单元及其所有“用户”。

#3


1  

Yes, it will also disable the warnings inside B.

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

翻译单元是.cpp文件,其所有包含的文件扩展为一个很棒的大文件。该pragma将持续到翻译单元的末尾,或者直到另一个#pragma警告更改设置。或者,如果您的编译器支持#pragma push和#pragma pop,它将持续到下一个#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.

'#pragma push'和'#pragma pop'允许您创建范围。此范围内的#pragma警告将应用于范围的末尾。