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

你说的曾经没有我的故事 提交于 2020-07-03 03:56:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!