How to declare packed struct (without padding) for LLVM?

You can use preprocessor directive to specify byte alignment for the struct so no padding will be done by the compiler:

#pragma pack(1)

typedef struct {
char        t1;
long long   t2;
char        t3;
} struct_size_test;

#pragma options align=reset

See the answer to this question on stackoverflow.


Did you actually try it? I just tested it on my machine, and __attribute__((packed)) compiled fine using clang.

Edit: I got the same warning ("Warning: packed attribute unused") for

typedef struct {
    int a;
    char c;
} mystruct __attribute__((packed));

and in this case sizeof(mystruct) was 8.

However,

typedef struct __attribute__((packed)) {
    int a;
    char c;
} mystruct;

worked just fine, and sizeof(mystruct) was 5.

Conclusion: it seems that the attribute needs to preceed the struct label in order to get this working.