Skip to main content
Associate
September 5, 2024
Question

STM32CubuIDE and RT-Thread Local variables are optimized out

  • September 5, 2024
  • 3 replies
  • 2006 views

Dear experts
I use STM32CubeIDE to compile the RT Thread project. When the optimization level is selected as "Optimize", local variables will be optimized,
Only when "Optimize for debug" is selected, local variables will not be optimized and the program can run normally,

May I ask why this is happening and what should I do?
Using 'Optimize for debug' will result in losing a lot of flash

asdss_0-1725539283206.png

asdss_1-1725539474281.png

asdss_2-1725539494085.png

 

 

3 replies

TDK
Super User
September 5, 2024

A variable being optimized out isn't a problem that needs to be addressed. This is expected behavior and you do not need to "correct" this.

If you want the variable to stay, but still want to compile with optimization, you can make the variable volatile.

volatile int index;

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
asdssAuthor
Associate
September 5, 2024

Thank you for your reply.
The local variable has been optimized, causing the program to run abnormally.
RT Thread is a third-party library with a lot of content; It still uses local variables in many places;
I added a volatile int index, and the index is normal, but there are still many others....
I compiled on Keil and used (- O3), and the program can run normally. Why doesn't work on STM32CubeIDE for "Optimize" ?
I don't think it should optimize my local variables.

asdss_0-1725540173497.png

 

Andrew Neil
Super User
September 5, 2024

@asdss wrote:

The local variable has been optimized, causing (sic) the program to run abnormally.


As @TDK said, that's a non-sequitur 

You have 2 observations:

  1. The local variable has been optimized-out;
  2. the program runs "abnormally"

It does not follow that (1) caused (2).

The "optimisation" could be simply that the loop is managed by a register - so there is no variable.

Or it could be that the loop has been "unwound" - so no counter is required.

What is the value of RT_Object_Info_Unknown ?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
TDK
Super User
September 5, 2024

> I don't think it should optimize my local variables.

But that's what compilers do. You can turn off optimization if you don't want that behavior.

Let's step back a bit: your program doesn't work with optimizations, you debug and step through and notice local variables are optimized out. This doesn't mean that local variables being optimized out is causing your problem. There is a software bug somewhere else that you need to find and address.

You should investigate what "run abnormally" means here in particular and debug the code to understand why that's happening. Local variables being optimized is not the problem.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Andrew Neil
Super User
September 5, 2024

@asdss wrote:

When the optimization level is selected as "Optimize",


There are also several other options:

AndrewNeil_0-1725545204335.png

For details of what they do, see:

https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

 

 

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.