Skip to main content
This topic has been closed for replies.
TOlli
TOlliBest answer
Senior
Mar 26, 2020 9:01 AM UTC

Hi,

To change the code optimization level (in a general sense) do the following:

Right-click the project under Project Explorer

Select properties

Go to C/C++ Build->Settings

On the right hand side of the window, select "Tool Settings"

There, under corresponding compiler, you have the options for debugging and optimisation. Under these two menus, you should be able to change debuggin and optimization level for the compiler.

I have attached a picture of one of my example projects to show what you are looking for.0693W000000UmsGQAS.jpg

Best regards.

9 replies

hs2
Visitor II
Mar 25, 2020 7:05 PM UTC

Do you really expect a helpful answer to this (non-)question ?

You missed any necessary context of your problem, right ;)

Javier1
Principal
May 3, 2022 7:50 AM UTC

i have the same question and this post helped me

hit me up in https://www.linkedin.com/in/javiermuñoz/
Javier1
Principal
Nov 17, 2022 3:29 PM UTC

aaaaand it helped me again

hit me up in https://www.linkedin.com/in/javiermuñoz/
TOlli
TOlliBest answer
Senior
Mar 26, 2020 9:01 AM UTC

Hi,

To change the code optimization level (in a general sense) do the following:

Right-click the project under Project Explorer

Select properties

Go to C/C++ Build->Settings

On the right hand side of the window, select "Tool Settings"

There, under corresponding compiler, you have the options for debugging and optimisation. Under these two menus, you should be able to change debuggin and optimization level for the compiler.

I have attached a picture of one of my example projects to show what you are looking for.0693W000000UmsGQAS.jpg

Best regards.

JHule.1
JHule.1Author
Associate II
Mar 26, 2020 12:59 PM UTC

Thank You, that worked, while watching the how to videos the guy says change the optimization level, but he was using Keil, that didn't do me any good when I was using STM32 Cube IDE, I looked for a user manual but ST didn't seem to have much.

JElli.1
Visitor II
Oct 4, 2020 8:57 AM UTC

I found that, if you have a specific function that must be optimized for speed, it works better to do it in your code like this:

#pragma GCC push_options
#pragma GCC optimize ("-Ofast")
void MyFunction()
{
 // The code for your function goes here...
}
#pragma GCC pop_options

The "#pragma" statements instructs the GCC compiler to optimize your function for speed.

hs2
Visitor II
Oct 4, 2020 10:15 AM UTC

Or alternatively and when using GCC attribute feature for a dedicated function:

void MyFunction() __attribute__((optimize("-Ofast")))

just for completeness

JElli.1
Visitor II
Oct 4, 2020 1:23 PM UTC

Thanks for the alternative hs2.

However in STM32CubeIDE that format gives the error: "attributes should be specified before the declarator in a function definition".

So I tried it like this >>

__attribute__((optimize("-Ofast"))) void MyFunction()
{
 // Code here
}
 

That worked perfectly. :)

Associate II
Sep 6, 2023 3:28 PM UTC

As stated in solution,

  • Go to Project/Properties/ C-C++ Build/Settings/Tool Settings tab
  • Under MCU/GCC Compiler select Optimization
  • Optimization Level drop down is on top right.

Note that there are 2 build configurations, Debug and Release. You select the configurations under Project/Build Configuration/Set Active.

Levels are apparently universal to all GCC compilers. From https://stackoverflow.com/questions/1778538/how-many-gcc-optimization-levels-are-there

DaveElectron_0-1694012015860.png

For debugging, I have been using the default (none) and have had no trouble with debugging. But if I do, I'll try -Og.