MDK-ARM优化程序大小

本文介绍如何通过MDK-ARM的配置选项实现程序大小的优化,包括使用跨模块优化去除未使用的函数以及设置每个函数单独的ELF节。此外,还提供了手动优化的方法,如精简代码逻辑和合理安排内存使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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和程序大小选择合适的地址来存数组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值