How to translate structured packaging from vC ++ to gcc

How to translate the following vC ++ packaging commands to gcc commands on Linux? I know how to do this for one structure, but how to do this for a number of structures?

#pragma pack(push, 1) // exact fit - no padding //structures here #pragma pack(pop) //back to whatever the previous packing mode was 
+1
c ++
source share
3 answers

To achieve this, you can add an attribute ((packed)) into individual data items. In this case, packaging is applied to the data item, so there is no need to restore the old mode.

Example: For structures:

 typedef struct _MY_STRUCT { }__attribute__((packed)) MY_STRUCT; 

For data members:

 struct MyStruct { char c; int myInt1 __attribute__ ((packed)); char b; int myInt2 __attribute__ ((packed)); }; 
+3
source share

gcc also supports these pragmas. See compiler docs: http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html

alternatively you can use more gcc-specific

 __attribute__(packed) 

Example:

 struct foo { int16_t one; int32_t two; } __attribute__(packed); 

http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Type-Attributes.html

+1
source share

According to http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html , gcc must support #pragma pack directly, so you can use it directly as it is.

gcc way to specify alignment __attribute__((aligned(x))) , where x requires alignment.

You can also use __attribute__((packed)) to specify a tightly packed structure.

Refer to http://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Type-Attributes.html

+1
source share

All Articles