Struct __ATTRIBUTE__ ((packed) results in "expected initializer before" error

I am trying to ensure a data structure is packed (for transmission in a UDP packet). All the examples I have found use this exact syntax, yet I am encountering this error.

struct __ATTRIBUTE__ ((packed)) dttmMsg {
  bool dttm_valid;
};

void setup() {
}

void loop() {
}
sketch_jan21a:1:33: error: expected initializer before 'dttmMsg'
 struct __ATTRIBUTE__ ((packed)) dttmMsg {
                                 ^~~~~~~

I am compiling for a ESP32 board, and I am using the latest Arduino IDE. Removing the attribute/packed bit, and the compile is fine.

Any suggestions would be greatly appreciated.

Thanks,

Allan.

I believe that syntax is specific to GCC. If a different compiler is used for ESP32 then you may have to use that compiler's syntax.

__ATTRIBUTE__ is very likely a macro which is expanding to nothing leaving the compiler with this...

struct ((packed)) dttmMsg {

__attribute__ is the keyboard for GCC; try that.

1 Like

That did it! Many thanks!

struct __attribute__ ((packed)) dttmMsg {
  bool     dttm_valid;
};
1 Like

Bear in mind that sizeof(bool) is not guaranteed to be 1 which makes it a poor choice for on-wire formats.

1 Like

I don’t think you can capitalize ATTRIBUTE, did you try lower case ?

Édit/ just realized I’m late to the question :slight_smile:

I also got confused by OP telling that they found many examples of it

Bear in mind that sizeof(bool) is not guaranteed to be 1 which makes it a poor choice for on-wire formats.

The actual struct I have is:

struct __attribute__ ((packed))dttm_msg_s {
  bool     dttm_valid;
  uint32_t dttm;
  uint32_t ntp_last_rcvd;
  uint8_t  tz;
};

I simplified the code in the original post to the bare minimum to show the compile issue. I can easily switch the bool to a uint_8 to be safer.

Thanks.

I also got confused by OP telling that they found many examples of it

So I go this morning to give an example of it in upper case, and every single example is in lower case! My mind must have been playing tricks on me.

Excellent choice.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.