包装结构与MinGW的调整 [英] Struct packing and alignment with mingw

查看:156
本文介绍了包装结构与MinGW的调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一台PC(mingw32的,32位拱)的嵌入式系统(STM32,Keil公司的μVision5,MDK-ARM)模拟code。 ARM编译器的对齐方式不符合我的台式机的的MinGW 的版本:

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 mingw build:

// ARM Code (ARM compiler uses __packed)
typedef __packed struct _file
{
    uint8_t var1;
    uint16_t var2;
} FILE;

// PC mingw gcc code trying to emulate layout above.
typedef struct __attribute__((packed, aligned(1))) _file
{
    uint8_t var1;
    uint16_t var2;
} FILE;



在源我做到以下几点: file.var1 = 0x22; file.var2 = 0xAA55将; ,然后写入存储器。当我在嵌入式系统上读取内存就说明 0x22,将0x55,和0xAA 。在Windows机器上读取 0x22,0xFF的,将0x55,和0xAA ,与2 第二字节填充。我怎样才能解决这种情况?


In the source I do the following: file.var1 = 0x22; file.var2 = 0xAA55; which is then written to memory. When I read the memory on the embedded system it shows 0x22, 0x55, 0xAA. On the Windows machine it reads 0x22, 0xFF, 0x55, 0xAA, with padding at the 2nd byte. How can I correct this behaviour?

推荐答案

我固定它由我自己 - >使用-mno-MS-位域有助于编译!上面的code是确实是正确的。有必要告诉MinGW的使用GCC的位域的组织,而不是微软的风格。虽然code能uncompileable与微软的编译器的话,我不会在这一点不在乎。

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.

这篇关于包装结构与MinGW的调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆