STM32 keil5编译优化

本来是写个IAP程序,Flash中只规划了4K字节,但是代码编写完后有6k,所以寻找优化方案,优化完在1.7k左右。

STM32 keil5编译优化,可以勾选一下几项:

1、Options for Taeget-->Target

勾选使用微库

STM32 keil5编译优化_第1张图片


2、Options for Taeget-->Target

①选择优化级别Level 3(-O3)

②勾选One ELF Section per Function


STM32 keil5编译优化_第2张图片

3、关于One ELF Section per Function的作用特别说明下:

编译器在处理一个 c 文件的时候呢,如果这个选项不选,那么这个C文件中的所有函数在编译后只会产生一个叫 .text 的输出节:

AREA ||.text||, CODE, READONLY, ALIGN=2 
如果选了呢,那么每个函数将会产生一个输出节,如果你用C写了一个叫PRTL_HeadGet()的函数,那么编译器将会产生一个叫 i.PRTL_HeadGet 的输出节,具体如下:
AREA ||i.PRTL_HeadGet||, CODE, READONLY, ALIGN=2
 PRTL_HeadGet PROC
勾选后,编译器产生的输出节,粒度小多了,便于找到没有使用的函数,将它从最后输出文件中删除。这样,你最后产生的可执行文件大小将会变小。

②代码大小对比:

没勾选:Program Size: Code=4152 RO-data=216 RW-data=36 ZI-data=6148  

勾选后:Program Size: Code=1376 RO-data=200 RW-data=0 ZI-data=6144  

③map文件对比:

工程右击open map file.

没勾选:

Removing Unused input sections from the image.该部分内容是去除项目中没有使用的函数。没勾选前是按照文件优化的。

==============================================================================


Removing Unused input sections from the image.


    Removing iap.o(.rev16_text), (4 bytes).
    Removing iap.o(.revsh_text), (4 bytes).
    Removing led.o(.rev16_text), (4 bytes).
    Removing led.o(.revsh_text), (4 bytes).
    Removing stm32f0xx_flash.o(.rev16_text), (4 bytes).
    Removing stm32f0xx_flash.o(.revsh_text), (4 bytes).
    Removing stm32f0xx_rcc.o(.rev16_text), (4 bytes).
    Removing stm32f0xx_rcc.o(.revsh_text), (4 bytes).
    Removing stm32f0xx_gpio.o(.rev16_text), (4 bytes).
    Removing stm32f0xx_gpio.o(.revsh_text), (4 bytes).
    Removing system_stm32f0xx.o(.rev16_text), (4 bytes).
    Removing system_stm32f0xx.o(.revsh_text), (4 bytes).
    Removing startup_stm32f030xc.o(HEAP), (512 bytes).

13 unused section(s) (total 560 bytes) removed from the image.

==============================================================================

勾选后:

看最后,94 unused section(s) (total 3760 bytes) removed from the image.由于每个函数一个输出节,所以优化是按照函数优化的,没有使用的函数都没有编译进去。而没有勾选时是按照文件优化的,只要该文件中有函数被使用,该文件中所有函数都会被编译进去。


Removing Unused input sections from the image.


    Removing iap.o(.rev16_text), (4 bytes).
    Removing iap.o(.revsh_text), (4 bytes).
    Removing iap.o(i.IAP_Delay), (6 bytes).
    Removing led.o(.rev16_text), (4 bytes).
    Removing led.o(.revsh_text), (4 bytes).
    Removing led.o(i.LEDTest), (42 bytes).
    Removing led.o(i.LED_flash), (40 bytes).
    Removing led.o(i.LED_temp_flash), (90 bytes).
   
Removing stm32f0xx_flash.o(.rev16_text), (4 bytes).
    Removing stm32f0xx_flash.o(.revsh_text), (4 bytes).

    Removing stm32f0xx_flash.o(i.FLASH_EraseAllPages), (52 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_GetFlagStatus), (20 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_GetPrefetchBufferStatus), (20 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_ITConfig), (24 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_BOOT0Config), (64 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_BOOT0SWConfig), (64 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_BOOTConfig), (64 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_EnableWRP), (140 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_Erase), (116 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_GetRDP), (20 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_GetUser), (16 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_GetWRP), (12 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_Launch), (20 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_Lock), (16 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_ProgramData), (56 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_RDPConfig), (92 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_SRAMParityConfig), (64 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_Unlock), (32 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_UserConfig), (72 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_VDDAConfig), (64 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_OB_WriteUser), (60 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_PrefetchBufferCmd), (24 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_ProgramHalfWord), (52 bytes).
    Removing stm32f0xx_flash.o(i.FLASH_SetLatency), (20 bytes).
    Removing stm32f0xx_rcc.o(.rev16_text), (4 bytes).
    Removing stm32f0xx_rcc.o(.revsh_text), (4 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_ADCCLKConfig), (44 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_AHBPeriphResetCmd), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_APB1PeriphClockCmd), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_APB1PeriphResetCmd), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_APB2PeriphClockCmd), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_APB2PeriphResetCmd), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_AdjustHSI14CalibrationValue), (20 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_AdjustHSICalibrationValue), (20 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_BackupResetCmd), (28 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_CECCLKConfig), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_ClearFlag), (20 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_ClearITPendingBit), (12 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_ClockSecuritySystemCmd), (28 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_DeInit), (92 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_GetClocksFreq), (376 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_GetFlagStatus), (52 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_GetITStatus), (20 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_GetSYSCLKSource), (16 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_HCLKConfig), (20 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_HSEConfig), (16 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_HSI14ADCRequestCmd), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_HSI14Cmd), (28 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_HSI48Cmd), (28 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_HSICmd), (28 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_I2CCLKConfig), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_ITConfig), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_LSEConfig), (32 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_LSEDriveConfig), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_LSICmd), (28 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_MCOConfig), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_PCLKConfig), (20 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_PLLCmd), (28 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_PLLConfig), (28 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_PREDIV1Config), (20 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_RTCCLKCmd), (28 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_RTCCLKConfig), (16 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_SYSCLKConfig), (20 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_USARTCLKConfig), (48 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_USBCLKConfig), (24 bytes).
    Removing stm32f0xx_rcc.o(i.RCC_WaitForHSEStartUp), (44 bytes).
    Removing stm32f0xx_rcc.o(.data), (16 bytes).
    Removing stm32f0xx_gpio.o(.rev16_text), (4 bytes).
    Removing stm32f0xx_gpio.o(.revsh_text), (4 bytes).
    Removing stm32f0xx_gpio.o(i.GPIO_DeInit), (112 bytes).
    Removing stm32f0xx_gpio.o(i.GPIO_PinAFConfig), (32 bytes).
    Removing stm32f0xx_gpio.o(i.GPIO_PinLockConfig), (26 bytes).
    Removing stm32f0xx_gpio.o(i.GPIO_ReadInputData), (4 bytes).
    Removing stm32f0xx_gpio.o(i.GPIO_ReadInputDataBit), (14 bytes).
    Removing stm32f0xx_gpio.o(i.GPIO_ReadOutputData), (4 bytes).
    Removing stm32f0xx_gpio.o(i.GPIO_ReadOutputDataBit), (14 bytes).
    Removing stm32f0xx_gpio.o(i.GPIO_StructInit), (24 bytes).
    Removing stm32f0xx_gpio.o(i.GPIO_Write), (4 bytes).
    Removing stm32f0xx_gpio.o(i.GPIO_WriteBit), (12 bytes).
    Removing system_stm32f0xx.o(.rev16_text), (4 bytes).
    Removing system_stm32f0xx.o(.revsh_text), (4 bytes).
    Removing system_stm32f0xx.o(i.SystemCoreClockUpdate), (108 bytes).
    Removing system_stm32f0xx.o(.data), (20 bytes).
    Removing startup_stm32f030xc.o(HEAP), (512 bytes).


94 unused section(s) (total 3760 bytes) removed from the image.


==============================================================================


你可能感兴趣的:(STM32)