Ignoring note: offset of packed bit-field without using "-Wno-packed-bitfield-compat"

6.4k views Asked by At

I have this warning pop up when I'm trying to compile the following union: 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 does not ignore that note. Is there a way to make #pragma work without having to use -Wno-packed-bitfield-compat since I want this warning ignored only for two of my eight unions?

2

There are 2 answers

0
Barry On BEST ANSWER

Just ran into a similar issue. It seems that gcc just doesn't like bitfields that cross the width of the type (as two does in the example)?

If you change all the types to uint16_t, gcc accepts:

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;
};

The layout is what you want it to be, even if the types of these members may not be.

1
JaeHoon Lee On
#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