太子爷小说网 > 杂集电子书 > icc avr制作和使用自定义库文件的范例 >

第1节

icc avr制作和使用自定义库文件的范例-第1节

小说: icc avr制作和使用自定义库文件的范例 字数: 每页4000字

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!



目标:1。 制作一个库文件libGetMax。a ,其中包含一个外部函数GetMax 。 函数GetMax的作用是判断两个输入参数(int 型)中的最大值,并返回最大值。
      2。 制作一个应用范例,使用库libGetMax。a 中包含的GetMax函数。
----------------------------------
步骤1: 库文件的生成
  为叙述方便,请在C盘根目录中新建一目录,并将其命名为libtest。以下将C:libtest简称为工作目录。
  在ICCAVR IDE环境中新建两个文件,分别命名为GetMax。c和GetMax。h,并保存至工作目录中。两文件内容如下所示:
**********************************
//GetMax。h的内容如下:
#ifndef __GETMAX_LIB

#define __GETMAX_LIB

extern int GetMax( int a; int b );

#endif
**********************************
//GetMax。c的内容如下:
#include 〃GetMax。h〃

int GettMax( int a; int b )

  return ( (a 》 b)?a:b );

**********************************
   
    执行File菜单下的pile File。。。项下的To Object项,将GetMax。c编译生成相应的目标文件GetMax。o 。
    执行命令ilibw …a libGetMax。a GetMax。o生成库文件。可执行文件ilibw。exe位于icc安装完后的系统目录(默认安装时在C:icc)的子目录bin中。为使用方便可将ilibw。exe拷贝至工作目录中使用。至此库文件libGetMax。a已生成,其对应的头文件为GetMax。h 。注意库文件的命名必须以“lib”字样开头,否则在IDE中编译下面的应用范例过程中自动链接时会报错(手动链接除外)。

----------------------------------
步骤2:应用范例的编写和编译
  在工作目录中建立一个工程Test。prj 。工程Test。prj中包含一个C源程序文件Test。c 。Test。C的源代码如下所示:
**********************************
//Test。c的源代码如下:

#include 〃GetMax。h〃
void main( void )

 int Result;Value1;Value2;
 Value1 = 0x10FF;
 Value2 = 0x0FFF;
 Result = GetMax( Value1; Value2);

**********************************
  
  然后,将前一步骤生成的库文件libGetMax。a拷贝至icc安装完后的系统目录(默认安装时在C:icc)的子目录lib中。再执行 Project菜单下的Option项,修改Target表单中的Additional Lib项,在文本框中输入GetMax (注意不能写成 libGetMax。a)。
    完成上述步骤后,就以编译和调试test。prj 。  
  如果在其它工程中需要使用上述库函数GetMax;只需拷贝所需的GetMax。h和libGetMax。a;并进行相应配置就可以了。
  也可以在已有的库中增加一些自定义的函数。详细参考ICCAVR IDE 中的在线帮助。
----------------------------------
参考资料:
1。ICCAVR IDE 中的在线帮助中的Librarian:
  A library is a collection of object files in a special form that the linker understands。 When a library's ponent object file is referenced by your program directly or indirectly; the linker pulls out the library code and links it to your program。 The standard supplied library is libcavr。a; which contains the standard C and AVR specific functions。 Other libraries; such as libstudio。a; override some functions in libcavr。a so that a different behavior can be achieved without changing your program code。 For example; by linking in libstudio。a; your program may use the Terminal IO window under AVR Studio。 Of course; the linking process is mostly transparent to you … by choosing the appropriate piler Options; the IDE generates the correct switches to the piler programs。

Nevertheless; there are times where you need to modify or create libraries。 A mand line tool called ilibw。exe is provided for this purpose (note: the PROFESSIONAL version may include capabilities that handle library files within the IDE; please inquire for details)。
Note that a library file must have the 。a extension。 See Linker Operations。
piling a File into a Library Module
Each library module is simply an object file。 Therefore; to create a library module; you need to pile a source file into an object file。 This can be done by opening the file into the IDE; and invoking the File…》pile File To Object mand。

Listing the Contents of a Library
On a mand prompt window; change the directory to where the library is; and give the mand 〃ilibw …t 〃。 For example;

ilibw …t libcavr。a

Adding or Replacing a Module

1。        pile the source file into an object module。
2。        Copy the library into the work directory
3。        Use the mand 〃ilibw …a  〃 to add or replace a module。

For example; the following replaces the putchar function in libcavr。a with your version。

cd icclibsrc。avr





copy iccliblibcavr。a        ; copy library 

ilibw …a libcavr。a iochar。o

copy libcavr。a icclib        ; copy back

ilibw creates the library file if it does not exist; so to create a new library; just give ilibw a new library file name。

Deleting a Module

The mand switch …d deletes a module from the library。 For example; the following deletes iochar。o from the libcavr。a library:

cd icclibsrc。avr

copy iccliblibcavr。a        ; copy library 

ilibw …d libcavr。a iochar。o        ; delete

copy libcavr。a icclib        ; copy back

  2。ICCAVR IDE 中的在线帮助中的piler Options: Target的Additional Libraries项介绍:
Additional Libraries … You may use other libraries besides the standard ones provided by the product。 For example; on our website is a library called libstk。a for accessing STK…200 peripherals。 To use other libraries; copy the files to the library directory and specify the names of the library files without the 〃lib〃 prefix and the 〃。a〃 extension in this box。 For example; 〃stk〃 refers to the libstk。a library file。 All library files must end with the 。a extension。
  

返回目录 回到顶部 0 0

你可能喜欢的