Structure Packing alignment


#1

Hi guys,

Do any of you know how to set packing alignment to structure in GCC (or should I say compiling for Wismo Target)? I’m using OpenAT 3.01 and i’ve only recently discovered, #pragma pack(1) only works in Debug mode but does not work in Wismo Target.

Hence, a structure eg.

#pragma pack(1)
typedef struct 
{
    char FirstMember;
    char SecondMember;
}Structure_T;

sizeof(Structure_T); would return 2 if packed into a single byte

BUT!!!

IT RETURNS 4 when compiled for Wismo Target.

So obviously it is packing into 4 byte lengths instead of 1. Does anyone know how to pack into a single byte length for Wismo Target?


#2

Hi Yul,

I hardly found it too, this is a known problem with gcc.
You should write :

#pragma pack(1)
typedef struct
{
    char FirstMember;
    char SecondMember;
} __attribute__((__packed__)) Structure_T;

but only when compiled for Wismo Target
So I do:

#ifndef TARGET_GCC_COMPILER
 #define ATTRIBUTE_PACKED
#else
 #define ATTRIBUTE_PACKED __attribute__((__packed__))
#endif

and ATTRIBUTE_PACKED is used on each structure

Best regards,


#3

Hello,

You can use “GNUC” macro, it is automatically defined with GCC compiler.

tom


#4

I’ve just been caught by this, too! :angry:

If it’s a “known problem with gcc”, is this documented somewhere? Do you have a link?
Any idea if there’s any plans to fix it?