Ignore notes: offset packed bit field without using "-Wno-packed-bitfield-compat"

I got this warning when I try 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 this note. Is there a way to make #pragma work without using -Wno-packed-bitfield-compat , since I want this warning to be ignored for only two of my eight unions?

+6
source share
2 answers

Just ran into a similar problem. It seems that gcc just doesn't like bit fields that intersect the type width (like two in the example)?

If you change all types to uint16_t , gcc will accept:

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

A layout is what you need, even if the types of these elements do not match.

0
source
 #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 
-1
source

Source: https://habr.com/ru/post/1261608/


All Articles