搜索
bottom↓
回复: 6

请教:IAR如何使一个未使用的函数不被优化掉

[复制链接]

出0入0汤圆

发表于 2016-8-27 10:55:47 | 显示全部楼层 |阅读模式
例如在工程中有一个函数,我已将该函数指定编译到单独划分的一块FLASH地址中,但是想在工程中以函数指针指向该地址的方式来调用此函数。

而如果该函数没有被显性地调用,编译器会视为该函数未使用自动将其优化掉了,生成的二进制代码中实际是没有它的。

是否有什么办法可以让这个函数不被优化,无论有没有使用都可以自动生成。

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 2016-8-27 10:59:41 来自手机 | 显示全部楼层
记得有个关键字好像是__root

出0入8汤圆

发表于 2016-8-27 11:21:09 | 显示全部楼层
你去帮助文档里面搜索一下 keep 关键字。

出0入0汤圆

发表于 2016-8-27 13:51:21 | 显示全部楼层
__root
The __root attribute can be used on either a function or a variable to ensure that, when the module containing the function or variable is linked, the function or variable is also included, whether or not it is referenced by the rest of the program.

By default only the part of the runtime library calling main and any interrupt vectors are root. All other functions and variables are included in the linked output only if they are referenced by the rest of the program.

The __root keyword is placed in front of the type, for example to place settings in non-volatile memory:

__root int settings[10];

The #pragma object_attribute directive can also be used. The following declaration is equivalent to the previous one:

#pragma object_attribute=__root
int settings[10];

Note: The __root keyword cannot be used in typedefs.

__root 关键字保证没有使用的函数或者变量也能够包含在目标代码中.

定义存放在__flash 空间的数据在程序编译时会自动生成代码嵌入到flash代码中,
对于程序没有使用也要求编译的数据(比如可以在代码中嵌入你的版本号,时间等)必须加关键字__root 限制。


Syntax --keep symbol
Parameters
symbol The name of the symbol to be treated as a root symbol

Description
Normally, the linker keeps a symbol only if it is needed by your application. Use this option to make a symbol always be included in the final application.

Project>Options>Linker>Input>Keep symbols




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

 楼主| 发表于 2016-8-27 15:06:24 | 显示全部楼层
谢谢楼上几位朋友的热心解答,经测试函数前加上__root关键字完美解决。

出0入0汤圆

发表于 2016-8-27 16:26:28 | 显示全部楼层
#pragma optimize=none

出0入0汤圆

发表于 2017-7-12 00:05:51 | 显示全部楼层
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-22 05:05

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表