µVISION: Changing optimization level for a project/group/file/function
Information in this knowledgebase article applies to:
In µVision is it possible to assign a different optimization level
to individual group or source file? And within a C file, is it
possible to assign different optimization levels to different
functions? If yes, how could I do that?
Yes, within µVision it is quite flexible to change optimization
levels for different groups, C files, or functions in a C file.
Optimization level for the entire project: on the toolbar in
µVision click Options for Target - C/C++ - Optimization, you
can choose different optimization levels for the entire target
project. Then all groups, component class and source files within
this target project will have such a defined optimization
Optimization level for a group/component class: even if you set
up an optimization level for the entire target project as described
above, you can still change the optimization level for the
individual group or component class. In order to do so, right
click on the folder of source files or the CMSIS component in the
project window, choose Options for Group/Component Class - C/C++
- Optimization, then you can set up a different
optimization level for this specific group or component class.
Optimization level for a C file: within a group folder or
component class, right click on a C file and choose Options for
File - C/C++ - Optimization, then you can set up a
different optimization level for this specific C file.
Optimization level for one or multiple C functions within a C
file: in a C file, if you want to optimize only one or several
functions instead of the entire C file, you can
directive to do so. e.g.
#pragma push /* Save existing optimization level */
#pragma O3 /* Optimization level now O3 */
#pragma pop /* Restore original optimization level */
In this way only the function foo will have optimization level
O3 and the other functions in this C file will remain the default
optimization level setting derived from this group or component
Please note: without #pragma
#pragma pop, only using
#pragma O3 will set up the optimization level for all of
the functions following this
#pragma O3 directive. Thus, if you put this
#pragma O3 directive at the beginning of the C file, then
all of the functions in this C file will have optimization level
Also note that this method using #pragma only works
with ARMCC v5.x compiler toolchain.
The following Discussion Forum threads may
provide information related to this topic.
Last Reviewed: Wednesday, September 9, 2020