cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeIDE: __attribute__((__packed__)) does not work; Build a File - no console output

ColdWeather
Senior

Hello,

Two questions.

First one: __attribute__((__packed__)) does not work: for a structure

typedef __attribute__((__packed__)) struct a_s {
 
  uint32_t M;
  uint16_t V;
  uint8_t Data[1];
 
} Type_A;

sizeof(Type_A) returns 8, not 7.

Second one: after STM32CubeIDE update to 1.10.1 when "Build a selected file" (*.c), the file seems to be processed, but NO build results more seen in the CDT Build Console.

Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions

Does not work, either.

P.S. Sorry, must revide! Yes, placing the attribute AFTER "}" just before the type name works! sizeof() returns 7.

typedef  struct a_s {
 
  uint32_t M;
  uint16_t V;
  uint8_t Data[1];
 
} __attribute__((__packed__)) Type_A;

View solution in original post

19 REPLIES 19
Piranha
Chief II

Maybe try the correct syntax...

An attribute specifier list may appear as part of a struct, union or enum specifier. It may go either immediately after the struct, union or enum keyword, or after the closing brace. The former syntax is preferred.

https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html

It (struct __attribute__((__packed__)) {... , - placing after struct) does not work either.

Does not work, either.

P.S. Sorry, must revide! Yes, placing the attribute AFTER "}" just before the type name works! sizeof() returns 7.

typedef  struct a_s {
 
  uint32_t M;
  uint16_t V;
  uint8_t Data[1];
 
} __attribute__((__packed__)) Type_A;

Piranha
Chief II

The "__packed__" attribute name is probably some leftover from old compiler versions and probably is deprecated. The current attribute name is "packed".

Both ((packed)) work, when modified according to the thread linked by @Nikita91 above: the __attribute__(()) should be not before or after struct but after } just before the type name:

typedef  struct a_s {
 
  uint32_t M;
  uint16_t V;
  uint8_t Data[1];
 
} __attribute__((packed)) Type_A;

Piranha
Chief II

I checked both names of "packed" and both correct attribute positions on GCC v10.3 and all combinations worked.

Semer CHERNI
ST Employee

Hello @ColdWeather​ 

For the first question:

I used the same syntax as you but the issue didn't reproduce; sizeof(TypeA) return 8.

typedef __attribute__((__packed__))  struct a_s {
 
  uint32_t M;
  uint16_t V;
  uint8_t Data[1];
 
} Type_A;

For the second question:

Check if you are pinning the console view

Does the issue is reproduced with other project in a different workspace.

Kind regards,

Semer.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

> both correct attribute positions on GCC v10.3 

Yes, gcc understands both variants with underscores and without. This is not new. Haven't checked clang, but gcc is quite an industry standard...

To the OP's question: use static_assert to verify the actual size. (#include <assert.h>)