ayates
January 22, 2022, 2:18am
1
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.
ayates
January 22, 2022, 3:37am
3
That did it! Many thanks!
struct __attribute__ ((packed)) dttmMsg {
bool dttm_valid;
};
Bear in mind that sizeof(bool) is not guaranteed to be 1 which makes it a poor choice for on-wire formats.
J-M-L
January 22, 2022, 7:59am
5
I don’t think you can capitalize ATTRIBUTE, did you try lower case ?
Édit/ just realized I’m late to the question
I also got confused by OP telling that they found many examples of it
ayates
January 22, 2022, 1:36pm
7
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.
ayates
January 22, 2022, 1:38pm
8
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.