Objective-c – How to declare packed struct (without padding) for LLVM

cobjective cstruct

It's possible to tell GCC it should not use padding for the struct. This is done using __attribute__((packed)).

typedef struct {

  uint8_t startSymbol;
  uint8_t packetType;
  uint32_t deviceId;
  uint16_t packetCRC;

} PacketData __attribute__((packed));

However, newest Xcode uses LLVM and does not recognize the attribute. How to define packed struct for LLVM?

The full description of the problem might be found here

UPDATE I'm using Xcode 4.5.1 for iOS which uses Apple LLVM 4.1 compiler. I'm getting "'packed' attribute ignored" warning in Xcode in the code example above.

Best Answer

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.

Related Topic