The #pragma pack(n).directive is the same as using the #pragma option specifically with the -a compiler option. n is the byte alignment that determines how the compiler aligns data in stored memory. For more information see the -a compiler option. #pragma pack can also be used with push and pop arguments, which provide the same functionality as the #pragma option directive using push and pop. The following table compares the use of #pragma pack with #pragma option.
#pragma pack #pragma option
#pragma pack(n) #pragma option -an
#pragma pack(push, n) #pragma option push -an
#pragma pack(pop) #pragma option pop
The #pragma pack directive also supports an identifier argument which must be used in combination with either push or pop.
#pragma pack with no arguments
#pragma pack()
Using #pragma pack with no arguments will set the packing size to the starting 朼X alignment (which defaults to 8). The starting -aX alignment is considered the alignment at the start of the compile AFTER all command-line options have been processed.
#pragma pack using a value for n
#pragma pack(8)
Using #pragma pack with a value for 'n' will set the current alignment to 'n'. Valid alignments for 憂?are: 1,2,4,8, and 16.
#pragma pack using push
#pragma pack(push)
Using #pragma pack with push will push the current alignment on an internal stack of alignments.
#pragma pack using push, identifier
#pragma pack(push, ident)
Using #pragma pack with push and an identifier will associate the pushed alignment with 'identifier'.
#pragma pack using push and n
#pragma pack(push, 8)
#pragma pack(push, ident, 8)
Using #pragma pack with push with a value for 'n', will execute pragma pack push or pragma pack push identifier, and afterwards set the current alignment to 'n'.
#pragma pack using pop
#pragma pack(pop)
Using #pragma pack with pop will pop the alignment stack and set the alignment to the last alignment pushed. If the pop does not find a corresponding push, the entire stack of alignments is popped, and a warning is issued, and the alignment reverts to the starting -aX alignment..
#pragma pack using pop, identifier
#pragma pop(pop, ident)
Using #pragma pack with pop and an identifier will pop the stack until the identifier is found and set the alignment to the alignment pushed by the previous corresponding #pragma pack(push, identifier). If the pop with identifier does not find the corresponding push with an identifier, the entire stack of alignments is popped, and a warning is issued to the user:
W8083: Pragma pack pop with no matching pack push
The alignment will then be reset to the starting -aX alignment.
#pragma pack using pop and n
#pragma pack(pop, 8)
#pragma pack(pop, ident, 8)
Using #pragma pack with pop and a value for 'n', will execute pragma pack pop or pragma pack pop identifier. Afterwards the current alignment is set to 'n', unless pop fails to find a corresponding push, in which case 'n' is ignored, a warning is issued, and the alignment reverts to the starting -aX alignment.
Error conditions
Specifying an 'identifier' without push or pop is an error.
Specifying an alignment different from 1,2,4,8,16 is an error.
Warning conditions
Using #pragma pop without a corresponding push issues a warning.
在所有的预处理指令中,#Pragma 指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作。#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的情况下,给出主机或操作系统专有的特征。依据定义,编译指示是机器或操作系统专有的,且对于每个编译器都是不同的。
其格式一般为: #Pragma Para
其中Para 为参数,下面来看一些常用的参数。
★(1)message 参数。 Message 参数是我最喜欢的一个参数,它能够在编译信息输出窗口中输出相应的信息,这对于源代码信息的控制是非常重要的。其使用方法为:
#Pragma message(“消息文本”)
当编译器遇到这条指令时就在编译输出窗口中将消息文本打印出来。
当我们在程序中定义了许多宏来控制源代码版本的时候,我们自己有可能都会忘记有没有正确的设置这些宏,此时我们可以用这条指令在编译的时候就进行检查。假设我们希望判断自己有没有在源代码的什么地方定义了_X86这个宏可以用下面的方法
#ifdef _X86
#Pragma message(“_X86 macro activated!”)
#endif
当我们定义了_X86这个宏以后,应用程序在编译时就会在编译输出窗口里显示“_X86 macro activated!”。我们就不会因为不记得自己定义的一些特定的宏而抓耳挠腮了。
(2)另一个使用得比较多的pragma参数是code_seg。格式如:
#pragma code_seg( ["section-name"[,"section-class"] ] )
它能够设置程序中函数代码存放的代码段,当我们开发驱动程序的时候就会使用到它。
★(3)#pragma once (比较常用)
只要在头文件的最开始加入这条指令就能够保证头文件被编译一次,这条指令实际上在VC6中就已经有了,但是考虑到兼容性并没有太多的使用它。
★(4)#pragma hdrstop表示预编译头文件到此为止,后面的头文件不进行预编译。BCB可以预编译头文件以加快链接的速度,但如果所有头文件都进行预编译又可能占太多磁盘空间,所以使用这个选项排除一些头文件。
有时单元之间有依赖关系,比如单元A依赖单元B,所以单元B要先于单元A编译。你可以用#pragma startup指定编译优先级,如果使用了#pragma package(smart_init) ,BCB就会根据优先级的大小先后编译。
★(5)#pragma resource "*.dfm"表示把*.dfm文件中的资源加入工程。*.dfm中包括窗体外观的定义。
★(6)#pragma warning( disable : 4507 34; once : 4385; error : 164 )
等价于:
#pragma warning(disable:4507 34) // 不显示4507和34号警告信息
#pragma warning(once:4385) // 4385号警告信息仅报告一次
#pragma warning(error:164) // 把164号警告信息作为一个错误。
同时这个pragma warning 也支持如下格式:
#pragma warning( push [ ,n ] )
#pragma warning( pop )
这里n代表一个警告等级(1---4)。
#pragma warning( push )保存所有警告信息的现有的警告状态。
#pragma warning( push, n)保存所有警告信息的现有的警告状态,并且把全局警告
等级设定为n。
#pragma warning( pop )向栈中弹出最后一个警告信息,在入栈和出栈之间所作的
一切改动取消。例如:
#pragma warning( push )
#pragma warning( disable : 4705 )
#pragma warning( disable : 4706 )
#pragma warning( disable : 4707 )
//.......
#pragma warning( pop )
在这段代码的最后,重新保存所有的警告信息(包括4705,4706和4707)。
★(7)pragma comment(...)
该指令将一个注释记录放入一个对象文件或可执行文件中。
常用的lib关键字,可以帮我们连入一个库文件。