How to tell GCC to set structure size boundary to 4 bytes through compiler settings (not pragma's)?

不羁的心 提交于 2019-12-10 22:52:20

问题


I want my c++ program compiled under GCC to have maximum alignment of 4 bytes (of members of structures). I really can do this through #pragma pack directive. However, it's uncomfortable in my case because the project is quite big, and I would need to make a single header with #pragma pack, that has to be included everywhere. Now, the gcc compiler has an option -mstructure-size-boundary=n documented here http://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html#ARM-Options , it says "Permissible values are 8, 32 and 64." In practice, when I try to set it to 4 bytes compiler throws a warning "structure size boundary can only be set to 8, 32 or 64" (surprising, eh?). But why I CAN set it to 4 through #pragma? Does anybody know how to set gcc struct alignment to other values than 8-32-64 through compiler settings?


回答1:


You understood the mstructure-size-option wrong. It refers to the size of the structure (as the name indicates). This does NOT relate to the alignment of the members. For that you could use (besides the pragma) the option -fpack-struct[=n].




回答2:


-mstructure-size-boundary=n specifies the alignment in bits while #pragma pack uses bytes.

4 bytes -> 32 bits.

See also gcc option -fpack-struct



来源:https://stackoverflow.com/questions/10277196/how-to-tell-gcc-to-set-structure-size-boundary-to-4-bytes-through-compiler-setti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!