如何用GCC编译器强制执行结构位顺序? [英] How to enforce the struct bit order with the GCC compiler?
问题描述
我想知道是否有一个GCC C编译器指令允许我确定打包结构的位顺序?一些喜欢的:
#pragma bit_order left
这种需求的基本原理是我有以下结构:
pre $ 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;
}项目;
} myStruct;
有了这个结构,我希望编译器能够这样打包它:
位次序:| 7 6 5 4 3 2 1 0 |
标签:| B0 B1 B2 B3 B4 B5 B6 B7 |
而不是GCC如何做:
位次序:| 7 6 5 4 3 2 1 0 |
标签:| B7 B6 B5 B4 B3 B2 B1 B0 |
我正在处理具有巨大头文件的MCU,这些头文件具有根据规定的硬件计算位偏移的结构地址。我希望GCC C编译器中有一个编译器指令,在我尝试翻转制造商提供的文件中的所有字段之前,对我进行位顺序交换。
你使用哪个版本的GCC和哪个平台?有一个编译指示可以做到这一点,但它无法在x86上以GCC 4开始。
#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
一些关于位域不好的细节:
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.
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
这篇关于如何用GCC编译器强制执行结构位顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!