如何使用 GCC 编译器强制执行结构体位顺序?
我想知道是否有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是哪个版本的 GCC?哪个平台?存在一个 pragma 可以解决这个问题,但它不适用于从 GCC 4 开始的 x86。
更多详细信息,请访问:
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.
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