6

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?

mans
  • 17,104
  • 45
  • 172
  • 321
  • From my understanding these should be the same. Don't forget to reset the alignment again with `#pragma pack()` at the end of the struct. Question might be duplicate of http://stackoverflow.com/questions/1537964/visual-c-equivalent-of-gccs-attribute-packed – Simon Kraemer Aug 25 '15 at 16:11
  • 1
    Possible duplicate of [What are the differences between #pragma pack(push, n)/#pragma pack(pop) and \_\_attribute\_\_((\_\_packed\_\_, aligned(n) )) on GCC?](http://stackoverflow.com/questions/33437269/what-are-the-differences-between-pragma-packpush-n-pragma-packpop-and-a) – Claudiu Oct 30 '15 at 15:34

1 Answers1

3

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

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 3
    #pragma pack can be used in gcc too (https://gcc.gnu.org/onlinedocs/gcc/Structure-Packing-Pragmas.html) – mans Aug 25 '15 at 16:07
  • 2
    #pragma pack is nonstandard extension, but woks with most compilers – Luka Rahne Aug 25 '15 at 16:09
  • 8
    Note also that `#pragma pack` takes effect from the point of definition and until another #pragma pack, while `__attribute__((__packed__))` is effective only for the definition it's attached to. This is important. – nos Aug 25 '15 at 16:19