Keil Logo

µVISION: Changing optimization level for a project/group/file/function

Information in this knowledgebase article applies to:

  • µVision IDE


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 level.
  • 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 use #pragma directive to do so. e.g. 
      #pragma push /* Save existing optimization level */
      #pragma O3   /* Optimization level now O3 */
      int foo(void)
      #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 class.

    Please note: without #pragma push and #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 O3.

    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

Did this article provide the answer you needed?
Not Sure
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.