当我尝试编译以下联合时,我会弹出此警告:
10:5: note: offset of packed bit-field 'main()::pack_it_in::<anonymous struct>::two' has changed in GCC 4.4
#pragma GCC diagnostic ignore "-Wpacked-bitfield-compat"
union pack_it_in {
struct
{
uint8_t zero : 3;
uint8_t one : 2;
uint8_t two : 6;
uint8_t three : 4;
uint8_t four : 1;
} __attribute__((packed)) u8_2;
uint16_t u16;
};
#pragma GCC diagnostic pop
#pragma不会忽略该注释。有没有办法让#pragma在不必使用-Wno-packed-bitfield-compat
的情况下工作,因为我希望仅在我的八个工会中有两个忽略此警告?
答案 0 :(得分:0)
就遇到了类似的问题。看来gcc只是不喜欢跨越类型宽度的位域(如示例中的two
一样)?
如果将所有类型都更改为uint16_t
,则gcc会接受:
union pack_it_in {
struct
{
uint16_t zero : 3;
uint16_t one : 2;
uint16_t two : 6;
uint16_t three : 4;
uint16_t four : 1;
} __attribute__((packed)) u8_2;
uint16_t u16;
};
布局就是您想要的布局,即使这些成员的类型可能不是。
答案 1 :(得分:-1)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpacked-bitfield-compat"
union pack_it_in {
struct
{
uint8_t zero : 3;
uint8_t one : 2;
uint8_t two : 6;
uint8_t three : 4;
uint8_t four : 1;
} __attribute__((packed)) u8_2;
uint16_t u16;
};
#pragma GCC diagnostic pop