cancel
Showing results for 
Search instead for 
Did you mean: 

how do i change code optimization

JHule.1
Associate II

I am using a nucleo-f401re board.

1 ACCEPTED SOLUTION

Accepted Solutions
TOlli
Senior

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.

View solution in original post

9 REPLIES 9
hs2
Senior

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

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

TOlli
Senior

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
Associate II

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
Associate III

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.

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

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

just for completeness

JElli.1
Associate III

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

i have the same question and this post helped me

we dont need to firmware by ourselves, lets talk

aaaaand it helped me again

we dont need to firmware by ourselves, lets talk
Dave.Electron
Associate II

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.