Keil ARM 编译的代码大小问题

本文介绍了在使用Keil ARM编译时遇到的代码大小不一致的问题,原因是编译器使用了`--feedback`选项,通过生成的反馈文件进行未使用函数的消除和减少编译所需的互操作性工作。多次编译可以利用反馈文件优化代码容量,特别是对关注代码大小的开发者而言,这是一个重要的知识点。
AI助手已提取文章相关产品:

最近,惊奇的发现对于一个ARM工程,使用Keil编译。连续编译几次,居然编译出的代码大小不一样。这是怎么回事呢?

首先,在target的option选项中的output选项页勾选“Create Batch File”,产生一个用于编译的批处理。

里面首先是设置环境变量,然后就是一行行的编译语句:

...........

"C:\Keil\ARM\BIN40\ArmCC" --Via ".\thc80f08d\opt\main.__i"

.........

打开这个__i文件

--feedback ".\THC80F08D\opt\JC.fed" --thumb -c --cpu ARM7TDMI -O3 --apcs=interwork --asm --interleave -I..\..\SRC\HAL\include -I..\..\SRC\JCAPI\include -I..\..\SRC\JCAPI\JNI -I..\..\SRC\JCSYS\SYSMM\include -I..\..\SRC\JCSYS\SYSCM\include -I..\..\SRC\JCSYS\SYSAM\include -I..\..\SRC\JCVM\include -I..\..\SRC\SYS\include -I..\..\SRC\HAL\THC80F08D\include -I..\..\SRC\JCSYS\SYSTF\include -I..\..\SRC\JCSYS\SYSGP\include -I..\..\SRC\JCSYS\SYSRM\include -I "C:\Keil\ARM\INC" -I "C:\Keil\ARM\INC\Atmel" -D__CHIP_ARM__ -D__SCP_02_SUPPORT__ -D__THC80F08D__ -o ".\THC80F08D\opt\main.o" --depend ".\THC80F08D\opt\main.d" "..\..\SRC\SYS\main.c"

可以看到就是ArmCC的命令参数。突然发现有个参数俺不太熟悉哦,--feedback是干嘛的?

打开Keil的帮助文档,找到这么一段:

--feedback=filename

2.1.53. --feedback=filename

This option enables the efficient elimination of unused functions, and on ARMv4T architectures, enables reduction of compilation required for interworking.

Syntax
--feedback=filename

Where:

filename

is the feedback file created by a previous execution of the ARM linker.

Usage

You can perform multiple compilations using the same feedback file. The compiler places each unused function identified in the feedback file into its own ELF section in the corresponding object file.

The feedback file contains information about a previous build. Because of this:

  • The feedback file might be out of date. That is, a function previously identified as being unused might be used in the current source code. The linker removes the code for an unused function only if it is not used in the current source code.

    Note

    • For this reason, eliminating unused functions using linker feedback is a safe optimization, but there might be a small impact on code size.

    • The usage requirements for reducing compilation required for interworking are more strict than for eliminating unused functions. If you are reducing interworking compilation, it is critical that you keep your feedback file up to date with the source code that it was generated from.


You have to do a full compile and link at least twice to get the maximum benefit from linker feedback. However, a single compile and link using feedback from a previous build is usually sufficient.


哈哈,还真是有新发现。编译器在编译过程中会回写一部分编译信息到feedback文件中,而这部分信息会正面作用于下一次的编译。

换而言之,对ARM工程,连续多编译几次,会有容量上的优化啊。像我一样对代码容量比较在意的同学一定要记住了~~~~



您可能感兴趣的与本文相关内容

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值