0

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

  • a library called myLib.a
  • a model called model.so 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 model.so 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

CdS
  • 13
  • 4
  • Better struct layout reached when small struct members follow large. Are you looking for https://stackoverflow.com/questions/14179748/what-is-the-difference-between-pragma-pack-and-attribute-aligned – 273K Jun 09 '21 at 14:35
  • Does this answer your question? [What is the difference between "#pragma pack" and "\_\_attribute\_\_((aligned))"](https://stackoverflow.com/questions/14179748/what-is-the-difference-between-pragma-pack-and-attribute-aligned) – 273K Jun 09 '21 at 14:35
  • Well my question is more focused on how to define the default alignment value, in order to be sure what the compiler will do when reaching a struct with no "#pragma pack" or "__attribute__ ((aligned))" instruction – CdS Jun 09 '21 at 15:03
  • This is described in the linked question. – 273K Jun 09 '21 at 15:13
  • Well I read the whole post pretty conscientiously and I only see cases where those attributes are defined. My question is "my code cannot contain #pragma pack nor __attribute__, can I set default alignement behaviour with a compiler flag". Maybe the answer is no – CdS Jun 09 '21 at 15:28
  • Where exactly are `#pragma pack(16)` and `#pragma pack(4)` coming from? – ssbssa Jun 10 '21 at 10:25
  • Well it does not appear anywhere in my code. I realised that getting the address of myStruct.var2 did not give the same value when called in myLib or from model, just like if there were different alignement instructions. – CdS Jun 10 '21 at 13:27

1 Answers1

0

See the answer in the question I posted in the comments https://stackoverflow.com/a/40043130/6752050

In "myLib" header file:

#pragma pack(push, 4)
typedef struct {
    char var1;
    int var2;
} myStruct;
#pragma pack(pop)  // disables the effect of #pragma pack from now on

or

typedef struct {
    char var1 __attribute__((packed,aligned(4)));
    int var2 __attribute__((packed,aligned(4)));
} myStruct;

This applies #pragma pack(4) to myStruct regardless of #pragma pack(16) used in "module".

273K
  • 29,503
  • 10
  • 41
  • 64
  • Thanks for your answers, I already applied your proposal as I needed to deliver a patch quickly. My point is to know if I can do this __without__ changing the source code (at compilation level). I struggle to understand why the same compiler do not pack things the same way. I though struct packing for x64 applications would be by 16 blocks by default.. But it does not seems to be that simple – CdS Jun 10 '21 at 09:11