对于某些编译器, struct 有一个打包说明符,例如:
RealView ARM compiler has "__packed" Gnu C Compiler has "__attribute__ ((__packed__))" Visual C++ has no equivalent, it only has the "#pragma pack(1)"
我需要一些我可以放在struct的定义中的东西.
有什么信息/黑客/建议吗?蒂娅...
对于某些编译器, struct 有一个打包说明符,例如:
RealView ARM compiler has "__packed" Gnu C Compiler has "__attribute__ ((__packed__))" Visual C++ has no equivalent, it only has the "#pragma pack(1)"
我需要一些我可以放在struct的定义中的东西.
有什么信息/黑客/建议吗?蒂娅...
您可以按如下方式为GNU GCC
和MSVC
定义Pack:
#ifdef __GNUC__
#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#endif
#ifdef _MSC_VER
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
#endif
这样使用:
PACK(struct myStruct
{
int a;
int b;
});