Keil的lib库函数的封装

在协同工作中,经常会有模块维护和代码封装的问题。把需要封装的代码打成一个lib无疑是一种很好的方式。

1.创建lib

创建一个lib很容易,只需要创建一个target,然后把需要封装的代码全部加进来,然后再Options of Target中选择Create Library,然后编译,因为是lib所以不需要链接,编译过了,你的lib就创建了。当然了,为了别人可以轻松的使用,请提供头文件支持哦。

2.使用lib

使用lib就更容易了,把lib和头文件加入你的工程,直接调用就是了。lib库会和你工程中其它编译后的obj一起链接,形成最后的目标文件。

3.注意事项

首先,Startup和中断处理程序不要封入LIB,这些程序会在链接的时候产生问题。具体的原因么,有点复杂,应该是中断程序的link机制有所不同的关系吧。

其次,Lib的文件要分的细一点,没有调用关系的两个函数不要放到同一个C文件中,因为LIB51在链接的时候是按模块来链接的,一个模块就对应一个C文件,假如链接器因为要使用你一个函数fA而引入了A模块,那么A模块中的另外的函数也会被引入,而另外的函数你又没有使用的话,那么就会引发Keil经典的UNCALLED FUNC的warning。这个warning在Keil的文档中说的好清楚了,我粘过来吧:

It is common practice during the development process to write but not call
additional functions.  While the compiler permits this without error, the
Linker/Locator does not treat this code casually because of the support for data
overlaying, and emits a warning message.
Interrupt functions are never called, they are invoked by the hardware.  An
uncalled routine is treated as a potential interrupt routine by the linker.  This
means that the function is assigned non-overlayable data space for its local
variables.  This quickly exhausts all available data memory (depending upon the
memory model used).
If you unexpectedly run out of memory, be sure to check for linker warnings
relating to uncalled or unused routines.  You can use the linker’s IXREF
directive to include a cross reference list in the linker map  (.M51)  file.

大意就是说,Keil的内存应用模式是指定地址的,也就是要根据调用关系来决定哪块地址可以被复用。对于这种没人调用的函数,Keil会认为是中断处理程序,并不能决定调用关系,所以此类uncalled函数的空间不能和其他的程序共享,也就是说,这函数用多少RAM,你就少多少RAM。那uncall多了会怎么样?----废话,当然是内存溢出了。

所以,lib的功能可以做的大而全,但是里面的模块一定要分的要多细,有多细,只有这样,你才能像在windows上用CRT一样舒服的使用LIB。

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值