I am emulating code from an embedded system (stm32, Keil µVision 5, MDK-ARM) on a PC (mingw32, 32bit arch). The alignment of the ARM compiler does not match my desktop m
attribute packed is broken on mingw32 compilers. Another option is to use pragma pack:
#pragma pack(1)
typedef struct _file
{
uint8_t var1;
uint16_t var2;
} FILE;
According to the GCC manual's examples, the attribute
should go after the struct's fields, i.e.:
// PC Code
typedef struct
{
uint8_t var1;
uint16_t var2;
} __attribute__((packed, aligned(1))) FILE;
I also dropped the pointless struct tag (_file
).
The above has a sizeof
value of 3
when I tested it quickly.
I fixed it by myself -> Compiling with -mno-ms-bitfields helps! The code above is indeed correct. It is necessary to tell mingw to use gcc's bitfield organisation instead of the microsoft style. Though the code can be uncompileable with microsoft compilers then, I do not care at this point.