MDK-ARM优化程序大小
MDK-ARM优化选项
KEIL中优化级别选项,在代码质量不够的情况下,是容易出问题,导致bug的。下面两个选项是安全的,根据官方文档描述,只是在链接时,去掉无用的代码段(Use Cross-Module Optimization去掉自己代码里无用的,通常用不到,因为不用的删掉就好,或者条件编译。。One ELF Section per Function去掉库里无用代码,这个效果挺明显的)。下面是原文链接。
选项位置分别为:
Options for Target - C/C++ - One ELF Section per Function
Options for Target - Target - Use Cross-Module Optimization
链接: 点这里.
In order to remove some unused functions in this case, the library objects need to be build with “–split_sections” (there is a check box One ELF Section per Function in Options for Target - C/C++ to enable it). This option will put each function of a module in its own section. So the linker is able to remove each individual function, because it is in its own section. So enabling this for your library will allow the linker to remove unused functions from the library.
Removing unused functions from the main application: In order to remove unused functions in a main application, you can enable Use Cross-Module Optimization in Options for Target - Target. By selecting this option linker makes a list of unused functions for compiler to use in the next build. Compiler takes this feedback file from prior build and uses it to place unused functions into their own ELF section in the object file. Then linker can place them in the unused sections and removes them from build.
手动优化
这好像没啥好说的,ROM只能优化一下代码逻辑,首先争取用尽量少的程序量完成尽量多的事情。然后把多次用到的代码写成函数调用。
RAM方面减少全局变量使用,用参数传递代替。把占用空间大的常量数组保存在FLASH中。目前只想到这些。。
方法如下:
static const uint8_t buf[len] __attribute__((at(0X800F000)))={0x80,0x80,0x80,0x80,0x80,0x80}
注释:0x8000000是内部flash的起始地址、那根据flash和程序大小选择合适的地址来存数组