Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 848
Oleg K Avatar asked Dec 21 '16 01:12

Oleg K


1 Answers

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.

like image 92
Barry Avatar answered Oct 27 '22 11:10

Barry