整理了一下msdn中关于pragma的一些用法,在此列出我认为是比较有用的一些pragma指令,并加上我的一些说明:
1 The following pragma causes the linker to search for the EMAPI.LIB library while linking. The linker searches first in the current working directory and then in the path specified in the LIB environment variable:
#pragma comment( lib, "emapi" )
-----------------------------------------------------------------------
2 When the compiler encounters a deprecated symbol, it issues C4995:
-----------------------------------------------------------------------
2 When the compiler encounters a deprecated symbol, it issues C4995:
void func1(void) {}
void func2(void) {}
int main() {
func1();
func2();
#pragma deprecated(func1, func2)
func1(); // C4995
func2(); // C4995
}
----------------------------------------------------------------------
3 The following code fragment uses the message pragma to display a message during compilation:
void func2(void) {}
int main() {
func1();
func2();
#pragma deprecated(func1, func2)
func1(); // C4995
func2(); // C4995
}
----------------------------------------------------------------------
3 The following code fragment uses the message pragma to display a message during compilation:
#if _M_IX86 == 500
#pragma message( "Pentium processor build" )
#endif
-----------------------------------------------------------------------
4 The warning-number-list can contain any warning numbers. Multiple options can be specified in the same pragma directive as follows:
#pragma message( "Pentium processor build" )
#endif
-----------------------------------------------------------------------
4 The warning-number-list can contain any warning numbers. Multiple options can be specified in the same pragma directive as follows:
#pragma warning( disable : 4507 34; once : 4385; error : 164 )
This is functionally equivalent to:
#pragma warning( disable : 4507 34 ) // Disable warning messages
// 4507 and 4034.
#pragma warning( once : 4385 ) // Issue warning 4385
// only once.
#pragma warning( error : 164 ) // Report warning 4164
// as an error.
// 4507 and 4034.
#pragma warning( once : 4385 ) // Issue warning 4385
// only once.
#pragma warning( error : 164 ) // Report warning 4164
// as an error.
Note that the compiler will add 4000 to any warning number that is between 0 and 999.
-----------------------------------------------------------------------
5 This pragma specifies packing alignment for structure, union, and class members,the parameter can only be 1,2,4,8,16,default is 8:
注:如果设置的值比结构体中字节最长的类型还要大,则这个变量(注意仅针对这一个变量)只按照它的字节长度对齐,即不会出现内存浪费的情况。请参见(4)。
-----------------------------------------------------------------------
5 This pragma specifies packing alignment for structure, union, and class members,the parameter can only be 1,2,4,8,16,default is 8:
注:如果设置的值比结构体中字节最长的类型还要大,则这个变量(注意仅针对这一个变量)只按照它的字节长度对齐,即不会出现内存浪费的情况。请参见(4)。
(1)
#pragma pack(1) //每个变量按照1字节对齐
struct A
{
char x; //aligned on byte boundary 0
int y; //aligned on byte boundary 1
}a;
sizeof(a)==5
#pragma pack(1) //每个变量按照1字节对齐
struct A
{
char x; //aligned on byte boundary 0
int y; //aligned on byte boundary 1
}a;
sizeof(a)==5
(2)
#pragma pack(2) //每个变量按照2字节对齐
struct A
{
char x; //aligned on byte boundary 0
int y; //aligned on byte boundary 2
}a;
sizeof(a)==6
#pragma pack(2) //每个变量按照2字节对齐
struct A
{
char x; //aligned on byte boundary 0
int y; //aligned on byte boundary 2
}a;
sizeof(a)==6
(3)
#pragma pack(4) //每个变量按照4字节对齐
struct A
{
char x; //aligned on byte boundary 0
int y; //aligned on byte boundary 4
}a;
sizeof(a)==8
#pragma pack(4) //每个变量按照4字节对齐
struct A
{
char x; //aligned on byte boundary 0
int y; //aligned on byte boundary 4
}a;
sizeof(a)==8
(4)
#pragma pack() //默认,相当于#pragma pack(8) 每个变量按照8字节对齐
struct A
{
char x; //aligned on byte boundary 0
int y; //aligned on byte boundary 4
}a;
sizeof(a)==8
但是这里y的大小是4字节,所以不会按照8字节对齐,否则将造成1个int空间的浪费
#pragma pack() //默认,相当于#pragma pack(8) 每个变量按照8字节对齐
struct A
{
char x; //aligned on byte boundary 0
int y; //aligned on byte boundary 4
}a;
sizeof(a)==8
但是这里y的大小是4字节,所以不会按照8字节对齐,否则将造成1个int空间的浪费
评论