如何使用 GCC 编译器强制执行结构体位顺序?

发布于 2024-11-24 08:09:37 字数 822 浏览 1 评论 0原文

我想知道是否有 GCC C 编译器指令允许我确定结构打包的位顺序?类似的东西:

#pragma bit_order left

这种需要的基本原理是我有以下结构:

struct {
       union {
             unsigned char BYTE;
             struct {
                 unsigned char B0: 1;
                 unsigned char B1: 1;
                 unsigned char B2: 1;
                 unsigned char B3: 1;
                 unsigned char B4: 4;
             }BIT;
       }ITEM;
} myStruct;

有了这个结构,我希望编译器以这种方式打包它:

Bit order: | 7  6  5  4  3  2  1  0 |
Label:     |B0 B1 B2 B3 B4 B5 B6 B7 |

而不是 GCC 是如何做的:

Bit order: | 7  6  5  4  3  2  1  0 |
Label:     |B7 B6 B5 B4 B3 B2 B1 B0 |

我正在处理具有巨大的 MCU头文件具有根据规定的硬件地址计算位偏移的结构。我希望 GCC C 编译器中有一个编译器指令,可以在我尝试翻转制造商提供的文件中的所有字段之前为我进行位顺序交换。

I was wondering if there is a GCC C Compiler directive that allows me to determine the bit order for packing of a structure? Something to the likes of:

#pragma bit_order left

The rationale for such a need is that I have the following structure:

struct {
       union {
             unsigned char BYTE;
             struct {
                 unsigned char B0: 1;
                 unsigned char B1: 1;
                 unsigned char B2: 1;
                 unsigned char B3: 1;
                 unsigned char B4: 4;
             }BIT;
       }ITEM;
} myStruct;

With this structure, I would like the compiler to pack it this way:

Bit order: | 7  6  5  4  3  2  1  0 |
Label:     |B0 B1 B2 B3 B4 B5 B6 B7 |

Rather than how GCC does it:

Bit order: | 7  6  5  4  3  2  1  0 |
Label:     |B7 B6 B5 B4 B3 B2 B1 B0 |

I am dealing with MCUs that have huge header files that have structures that compute bit offsets according to stipulated hardware addresses. I am hoping that there is a compiler directive in GCC C Compiler that does the bit order swap for me before I attempt the flip all the fields in the manufacturer supplied file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2024-12-01 08:09:38

您使用的是哪个版本的 GCC?哪个平台?存在一个 pragma 可以解决这个问题,但它不适用于从 GCC 4 开始的 x86。

#pragma reverse_bitfields on

更多详细信息,请访问:

http://groups.google.com/group/gnu.gcc.help/browse_thread/thread/747918655affa5c0?pli=1

如果您不介意重建 GCC,则所有相关构建设置在这里(搜索bitfield):

http://gcc.gnu.org/onlinedocs/gccint/Storage-Layout.html

有关位域不良的一些详细信息:

C/C++:强制位字段顺序和对齐

Which version of GCC are you using and which platform? A pragma exists that may do the trick, but it doesn't work on x86 starting with GCC 4.

#pragma reverse_bitfields on

More details at:

http://groups.google.com/group/gnu.gcc.help/browse_thread/thread/747918655affa5c0?pli=1

If you don't mind rebuilding GCC, all the relevant build settings are here (search for bitfield):

http://gcc.gnu.org/onlinedocs/gccint/Storage-Layout.html

Some details about bitfields being bad:

C/C++: Force Bit Field Order and Alignment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文