2024-09-05 05:32 AM
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
2024-09-05 05:35 AM
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;
2024-09-05 05:43 AM
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.
2024-09-05 06:28 AM
> 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.
2024-09-05 06:42 AM
@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:
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 ?
2024-09-05 07:18 AM - edited 2024-09-05 07:18 AM
@asdss wrote:When the optimization level is selected as "Optimize",
There are also several other options:
For details of what they do, see:
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
2024-09-05 06:09 PM
Thank you for your reply.
You're right, optimizing local variables is not a problem.
I delved deeper into the comparison and found that the content pointed to by 'rt_oobject_comtainer' seems to be incorrect,
Please refer to the following figure. I compared the pointing values of rt_ort_comtainer under Debug and Optimize (O1) optimization levels
Under Optimize (O1) optimization level:
After obtaining the program information&rt_oobject_comtailer [2], it cannot exit the for loop, which will generate a hardware error interrupt approximately 5 times.
The content of the rt_oobject_comtailer linked list is generated to: I am not very familiar with what the compiler has done to cause the content of rt_oobject_comtailer to deviate.
Please help me, thank you
2024-09-05 06:12 PM
Thank you for your reply.
RT_Object_Info_Unknown = 3;
I have updated the reply information above, please help me check again, thank you.