How to set default struct alignment value as compilation parameter (GCC)

  64-bit, c++, gcc, pack

I am currently working on a Linux x64 gcc project splitted in two part:

  • a library called myLib.a
  • a model called linking the previous library

Those two parts are not compiled at same moment and not in the same environment (I don’t have control on compilation instructions).
Both uses struct defined in some header an share instances thanks to pointers

My problem is that the same struct defintion, let’s say

typedef struct {
    char var1;
    int var2;
} myStruct;

Will be compiled with implicit

#pragma pack(16)

in the "model" environment whilst in "myLib" it is compiled with implicit

#pragma pack(4)

Then the struct field myStruct.var2 may not point at the same memory address in "myLib" and in "model"

I don’t find how to tell the compiler to use 16 byte alignment on every struct (and avoid explicitly writing it in my code)

My compilation command for "myLib" looks like this:

g++  -D __x86_64__ -Wall -fpic  -c ./libcode.cpp -o ./obj/Release_x64/libcode.o
ar -r -s ./bin/Release_x64/myLib.a ./obj/Release_x64/libcode.o

Thanks for your help

Source: Windows Questions C++