cancel
Showing results for 
Search instead for 
Did you mean: 

I am running STM32CubeIDE 1.9 Is it possible to have some code files run at one optimization, and other files run at a different optimization?

KiptonM
Lead

My code is larger than expected and I am close to the 32K program boundary of my device. When I optimize for debugging or have no optimize I cannot test new code.

When I optimize for size I have plenty of room for the new code, but single stepping through the code to find a bug is a problem because the optimization jumps all over the place.

So can I optimize for size the files that I know are good, and optimize for debugging or no optimize the files I am working on?

It appears it is optimize by the whole project. If I change the optimization, it recompiles the whole project to that optimization.

The new rev of the board increases the memory from 32K to 64K but that is not going to happen for a couple of months, and I need this version working before they will release the new board. (Chicken and Egg problem)

1 ACCEPTED SOLUTION

Accepted Solutions
Bob S
Principal

There are a couple of ways:

1) In the "Project Explorer" window, right-click on a source file (.c or .cpp) and go to "Properties". It will bring up a dialog similar to the general project properties. Under "C/C++ Build" then "Settings" change the optimize settings for that file.

2) You can also force optimization for individual functions by using the "__attribute__((optimize("***")))" string on the function declaration line, where "***" is the type of optimization, for example "Ofast", "Os", "O1", etc. (that is the letter "Oh" not number zero).

__attribute__((optimize("Os"))) void MyFunct( int param )
{
   // function code here
}

View solution in original post

3 REPLIES 3
Bob S
Principal

There are a couple of ways:

1) In the "Project Explorer" window, right-click on a source file (.c or .cpp) and go to "Properties". It will bring up a dialog similar to the general project properties. Under "C/C++ Build" then "Settings" change the optimize settings for that file.

2) You can also force optimization for individual functions by using the "__attribute__((optimize("***")))" string on the function declaration line, where "***" is the type of optimization, for example "Ofast", "Os", "O1", etc. (that is the letter "Oh" not number zero).

__attribute__((optimize("Os"))) void MyFunct( int param )
{
   // function code here
}

MM..1
Chief III

So can I optimize for size the project, and optimize for debugging or no optimize the part

#pragma GCC push_options
#pragma GCC optimize ("O0")
 
your code
 
#pragma GCC pop_options

but i mean better is debug full project with set optimize for example O3

Debugging with optimizations on is hard because the code may not follow your code line for line, and you may not be able to look at a variable that may have gotten optimized out.

When you do not have to debug anymore then it is best to optimize.