问题
I am porting a code which runs perfectly on Linux to windows visual c++. I have this code in Linux:
struct exif_desc
{
uint16_t tag;
uint16_t type;
uint32_t length;
uint32_t value;
}
__attribute__((__packed__));
I am getting error on windows:
'__packed__' : undeclared identifier
I am wondering if I can fix this error by using
#pragma pack(1)
is there any difference between them? Is there any syntax that can be used in Linux and Windows for this attribute?
回答1:
__attribute__
is a GCC extension, specific to GCC (and other compilers which attempts to be compatible with GCC).
#pragma pack
is originally a Visual C++ compiler specific extension. It has, as noted by commenters, been implemented in GCC as well for VC++ compatibility.
Normally you can't use extensions in one compiler in another compiler. Case in point: __attribute__
doesn't exist as an extension in the Visual C++ compiler.
来源:https://stackoverflow.com/questions/32208805/what-is-the-difference-between-attribute-packed-and-pragma-pack1