在協同工作中,經常會有模塊維護和代碼封裝的問題。 把需要封裝的代碼打成一個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。

 

文章出處: http://blog.csdn.net/phenixyf/article/details/8549487

arrow
arrow
    全站熱搜

    Chi Learning 發表在 痞客邦 留言(0) 人氣()