what is the difference between __attribute__((__packed__)); and #pragma pack(1)

4.3k views Asked by At

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

There are 1 answers

3
Some programmer dude On

__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.