如何通过编译器设置(不是pragma' s)告诉GCC将结构大小边界设置为4个字节? [英] How to tell GCC to set structure size boundary to 4 bytes through compiler settings (not pragma's)?

查看:91
本文介绍了如何通过编译器设置(不是pragma' s)告诉GCC将结构大小边界设置为4个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在GCC下编译的c ++程序的最大对齐长度为4个字节(结构成员).我真的可以通过#pragma pack指令来做到这一点.但是,对于我来说,这很不舒服,因为该项目很大,而且我需要使用#pragma pack制作一个标头,该标头必须包含在所有地方.现在,gcc编译器具有-mstructure-size-boundary = n选项,在此处

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?

推荐答案

您理解mstructure-size-option错误.它指的是结构的大小(顾名思义).这与成员的对齐方式无关.为此,您可以使用(除了编译指示)选项 -fpack-struct [= n] .

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].

这篇关于如何通过编译器设置(不是pragma' s)告诉GCC将结构大小边界设置为4个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆