您的位置:首页 > 编程语言 > C语言/C++

Mingw-gcc结构体取消对齐

2014-03-28 14:56 543 查看
         在gcc中,一般是可以通过__attribute__((packed))属性来取消对齐的,昨天在写个小工具是发现在使用code::block+mingw-gcc时却是死活不行了,无奈之下,只有在虚拟机里面使用gcc编译了。

        今天差不多忙完了,来查查原因来了,原来MinGW-GCC是默认启动了兼容MS的特性的,

        相关的GCC的说明查看http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html

摘抄如下


Two attributes are currently defined for i386 configurations: ms_struct and gcc_struct.

ms_struct
gcc_struct
If packed is used on a structure, or if bit-fields are used it may be that the Microsoft ABI packs them differently than GCC normally packs them. Particularly when moving packed data between functions compiled with GCC and the native Microsoft compiler (either via function call or as data in a file), it may be necessary to access either format.

Currently -m[no-]ms-bitfields is provided for the Microsoft Windows X86 compilers to match the native Microsoft compiler.

所以在MinGW-GCC中,在用于结构体的对齐时,应该添加的__attribute__((gcc_struct, packed)) 。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息