谁能告诉我在 C 和 Ada 中使用 pragma,如果可能的话,提供一些例子。
5 回答
C99 中有三个标准的 pragma:
#pragma STDC FP_CONTRACT on-off-switch
#pragma STDC FENV_ACCESS on-off-switch
#pragma STDC CX_LIMITED_RANGE on-off-switch
其中'on-off-switch' 是ON、OFF、DEFAULT 之一。
这些可以在编译时用于以神秘的方式修改编译器的行为(这些与 C99 浮点行为有关)。标准为标准 pragma 保留 STDC;其他人可以使用他们喜欢的任何东西。
也有非标准的编译指示 - 正如 Samuel Klatchko 所指出的那样。
基本上,它们是一种让编译器以半标准方式执行非标准操作的方法。一个例子是 ' #pragma pack
',这意味着创建的结构在成员之间没有填充,即使这意味着对这些成员的访问将是次优的(大概空间比时间更重要)。这不是一个特别好的主意(尽管使用它的人会反对);但这是一个普遍感知的要求,因此编译器通常支持它。
我已经用 C 编程了 - 哦,25 年了,还有一点空闲。我不需要使用 pragma 一次。我玩过它几次,但从来没有真正需要使用它。也许我很幸运。
在 C 中,大多数 pragma 是特定于编译器/平台的(尽管有一些类似#pragma once
的被广泛实现)。
这是一个关于gcc pragmas的页面和另一个关于Microsoft VC pragmas的页面。
In Ada, "A pragma is a compiler directive." Many are defined by the language, but implementation-defined pragmas are permitted. The Rationale for Ada 2005 offers many examples.
...对于务实的程序员 ;-)
`#pragma'指令是 C 标准指定的方法,用于向编译器提供超出语言本身传达的信息的附加信息。该指令的三种形式(通常称为 pragma)由 1999 C 标准指定。AC 编译器可以自由地将它喜欢的任何含义附加到其他 pragma 中。
http://gcc.gnu.org/onlinedocs/cpp/Pragmas.html
http://msdn.microsoft.com/en-us/library/d9x1s805%28VS.71%29.aspx
You can basically think of them as commands to the compiler. These commands are used during compilation process.
Example
#include<windows.h>
#pragma comment("lib","shell32.lib")
wmain(){
......
}
In the above example you are basically asking the linker to include shell32.lib automatically while linking you program. without that you have to manually specify shell32.lib in the command line of cl.exe
Another example would be you can ask the compiler to functions in the final executable...
#pragma auto_inline(on)
int functionToBeInlined()
{
//.....
}
#pragma auto_inline(off)
All occurrences of the above function will be inlined.
pragma reference of VC++ compiler
each compiler has there own specific pragmas