当前位置: 代码迷 >> 嵌入开发 >> arm-linux-gcc下的字节对齐有关问题
  详细解决方案

arm-linux-gcc下的字节对齐有关问题

热度:1205   发布时间:2016-04-28 09:04:50.0
求助: arm-linux-gcc下的字节对齐问题
2套不同的协议。 对内协议使用默认的 4字节对齐, 网络通讯协议使用单字节对齐。

希望对 2种协议定义的结构体使用不同的字节对齐方式。

查了一下百度,试了几种网上提供的方法好像都无效。

1. typedef struct {
unsigned char x ;
int y;
}STRUCT_TEST __attribute__((packed,aligned(1)));

2. typedef struct STRUCT_TEST{
unsigned char x ;
int y;
} __attribute__((packed,aligned(1)));

3.#pragma pack(1)
  typedef struct {
unsigned char x ;
int y;
}STRUCT_TEST;
 #pragma pack()
以上3种方式得到的 sizeof 都为8.

而如果修改makefile 加上 -fpack-struct 后。
STRUCT_TEST 长度为5没错了,但是又影响了另外一些需要4字节对齐的结构体。

很头痛,希望大家能提供宝贵意见

------解决方案--------------------
应该是这样吧,自定的
// Loss of optical port
typedef struct
{
short index __attribute__ ((packed));
short opticalPortLoss[32] __attribute__ ((packed));
} __attribute__ ((packed)) PORT_LOSS;
默认的去掉__attribute__ ((packed))应该可以了,现在没环境,明天可以测 一下。