Skip to main content
ColdWeather
Senior
August 16, 2022
Solved

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

  • August 16, 2022
  • 8 replies
  • 9501 views

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?

This topic has been closed for replies.
Best answer by ColdWeather

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;

8 replies

Nikita91
Lead II
August 16, 2022
ColdWeather
ColdWeatherAuthorAnswer
Senior
August 17, 2022

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
Principal III
August 16, 2022

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

ColdWeather
Senior
August 17, 2022

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

Piranha
Principal III
August 17, 2022

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

ColdWeather
Senior
August 17, 2022

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
Principal III
August 17, 2022

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

Pavel A.
Super User
August 23, 2022

> 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>)

ColdWeather
Senior
August 24, 2022

Thanks! I inserted

static_assert(sizeof(Type_A) == 7);

for the case in my code...

Semer CHERNI
ST Employee
August 22, 2022

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.
ColdWeather
Senior
August 24, 2022

Thanks for your replies.

  1. sizeof(Type_A) sould return 7 for the structure, not 8. It returns the desired/correct 7 only, if __attribute__((packed)) or __attribute__((__packed__)) is placed just before Type_A. I talked to a friend working with gcc (some other IDE), too, and he confirmed having had faced the same problem and applied the same solution.
  2. Well, in another workspace the console shows the build results of a single file as expected and as it has been before. I have no idea, what option of the IDE I might have changed under the "broken" workspace... When building the entire project (not a single file only), the console shows the results as usual.
Semer CHERNI
ST Employee
August 24, 2022

About the second question I think that you accidently clicked on "Pin Console".

0693W00000Stg5AQAR.png 

Check if the button shown in the screenshot is not pressed on any console in your workspace.

Kind regards,

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.
ColdWeather
Senior
August 24, 2022

In the "good" workspace it does not matter, pinned or not: I see the output of the builder anyway. The main issue is no more the console but the fact, under the "bad" workspace the internal builder crashes:

0693W00000StgD9QAJ.pngLater, since for building of a single file always the internal builder seems to be engaged, it crashes silently, and the console shows nothing...

Semer CHERNI
ST Employee
August 25, 2022

Hello @ColdWeather​ 

Could you please send me the workspace .log file to be able to further do analysis the issue.

This file is found under the workspace directory under ".metadata".

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.
ColdWeather
Senior
August 25, 2022

Thank you for helping me!

I discovered an interesting thing: under the "bad" workspace I have actually two projects: "Basic" and "Mowitec", and the last one can be switched to the internal builder w/o any issue! Thus, it must be some project setting(s) for "Basic" that make(s) the internal builder crash.

I closed the IDE, deleted *.log files, started again and built both "Mowitec" and "Basic". The log file is under the link:

https://drive.google.com/file/d/1WevKLwnb2cl6cEYKfyFCk7fLMwwM7zX2/view?usp=sharing

Semer CHERNI
ST Employee
August 29, 2022

Hello

We do not support the CDT internal builder.

May I ask why you want to use the Internal builder?

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.
ColdWeather
Senior
August 31, 2022

I'm afraid, you did not read my previous report regarding two projects under the workspace with the issue. Regardless of your assert, if you support or not the internal builder, the symptoms are that the IDE does support both builders, and when building/translating a single file the internal builder is engaged regardless of the project setting "use internal" or ".. external" builder. I have no issue to use the external one (and I do it!), but the point is, I cannot build a single file (what I used to as a convenient way to check the syntax quickly) because the IDE seems to engage always the internal one for this purpose that crashes in my project "Basic". At the same time, in the same workspace(!), another similar project generates no errors when building a single file. Conclusions:

  • your IDE does support internal CDT
  • some broken project setting under "Basic" makes something to crash (see the log).