制作和使用自定义库文件的范例: 目标: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菜单下的Compile 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 component 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 Compiler Options, the IDE generates the correct switches to the compiler programs. Nevertheless, there are times where you need to modify or create libraries. A command 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. Compiling a File into a Library Module Each library module is simply an object file. Therefore, to create a library module, you need to compile a source file into an object file. This can be done by opening the file into the IDE, and invoking the File->Compile File To Object command. Listing the Contents of a Library On a command prompt window, change the directory to where the library is, and give the command "ilibw -t ". For example, ilibw -t libcavr.a Adding or Replacing a Module 1. Compile the source file into an object module. 2. Copy the library into the work directory 3. Use the command "ilibw -a " to add or replace a module. For example, the following replaces the putchar function in libcavr.a with your version. cd \icc\libsrc.avr copy \icc\lib\libcavr.a ; copy library ilibw -a libcavr.a iochar.o copy libcavr.a \icc\lib ; 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 command switch -d deletes a module from the library. For example, the following deletes iochar.o from the libcavr.a library: cd \icc\libsrc.avr copy \icc\lib\libcavr.a ; copy library ilibw -d libcavr.a iochar.o ; delete copy libcavr.a \icc\lib ; copy back 2.ICCAVR IDE 中的在线帮助中的Compiler 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.