2
votes

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?

2

2 Answers

3
votes

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

4 bytes -> 32 bits.

See also gcc option -fpack-struct

3
votes

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