xiaoke 发表于 2004-11-28 10:15:37

ICCAVR中如何使用自己的库文件?

1:朋友的子程序封装为一个扩展名为.a的库文件,是否将其COPY到C:ICC/LIB目录就可以直接使用?还是要做什么设置?

2:如果我想把我的几个子程序也封装到这个库文件里,应如何做?

以上两个问题查了手里的“AVR单片机C语言开发入门指导”和“AVR系列单片机C语言编程与应用实例”

都没有提到。是因为这些问题太初级了吗?

各位DX指点一下吧。谢谢

Gorgon_Meducer 发表于 2004-11-28 10:36:36

# include <yourfiles.a>



直接将你的程序粘贴在库里就行了,注意函数和变量的外部声明问题哦
<font color=red>鄙视一下自己以前的回答。

xiaoke 发表于 2004-11-28 11:51:13

我的子程序如何放到库里?库可是没有源码呀。是否要把我的子程序先编译成OBJ文件?然后呢?

Gorgon_Meducer 发表于 2004-11-28 13:09:56

库本来就是存放着源代码的,扩展名为.h

难道我理解你的意思理解错了?

你的意思难道是程序间参数的传递?
<font color=red>鄙视一下自己以前的回答。

yaya001 发表于 2004-11-28 13:19:42

21ic的历史里有的

xiaoke 发表于 2004-11-28 14:02:17

不是.h,只有一个.a文件

Gorgon_Meducer 发表于 2004-11-28 15:44:16

直接INCLUDE
<font color=red>鄙视一下自己以前的回答。

fatboy 发表于 2004-11-28 23:28:54

一般每个库(.a文件)都应有一个头文件(.h文件)对应。头文件中声明库文件中有哪些外部可调用的函数、变量(变量一般很少出现,对其操作一般都用函数封装)、以及一些相关的常量声明。

在一个程序中引用自定义的库,应按以下步骤进行:

首先在源程序中包含相应的头文件,将库文件拷贝至指定的库目录中。

然后执行Project菜单下的Option项,修改Target表单中的Additional Lib项,加入自定义的库名(不用加扩展名)。

接下来就可以编译试一试了。

----------------

请参考以下在线帮助



Compiler Options: Target



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.



----------------

我过一会儿做个范例给大家看看吧。

fatboy 发表于 2004-11-29 02:25:34

制作和使用自定义库文件的范例:

目标: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 <library>". 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 <library> <module>" to add or replace a module.



For example, the following replaces the putchar function in libcavr.a with your version.



cd \icc\libsrc.avr



<modify putchar() in iochar.c>



<compile iochar.c into iochar.o>



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.

  

fatboy 发表于 2004-11-29 02:34:44

to:阿莫

上面的算不算一个应用范例,可以的话给我开放应用测试区的权限吧,有时间我会再贡献一些的。谢了。

-----------------

大家有兴趣的话,可以仔细研究研究库文件和目标文件的关系,这两种类型的文件都是文本文件。仔细一看大家就会发现ilibw.exe非常懒,没干啥事,就把目标代码分别打了打包。

xiaoke 发表于 2004-11-29 09:12:14

谢谢。但是我的问题是:只有朋友给的new.a和new.h文件,没有new.c文件,想把我的几个子程序(my.c)追加(封装)到new库里。应如何操作?查遍了资料都没有。是否不可能?

armok 发表于 2004-11-29 09:56:52

fatboy 发表于 2004-11-29 11:20:20

to xiao ke:

可以追加,先将my.c编译成my.o,再用ilibw将my.o追加到new.a中,最后修改new.h文件。

----------------

追加命令:

ilibw -a new.a my.o

--------------------------------

详细见ilibw.exe的帮助说明

xiaoke 发表于 2004-11-29 14:20:07

谢谢,明白了

armok 发表于 2004-11-29 14:40:43

lovexe 发表于 2008-10-9 11:57:12

我为什么编译不同呢?



C:\icc\bin\imakew&nbsp;-f&nbsp;Test.mak

&nbsp;&nbsp;&nbsp;&nbsp;iccavr&nbsp;-c&nbsp;-IC:\icc\include\&nbsp;-e&nbsp;-DATMEGA&nbsp;-DATMega128&nbsp;&nbsp;-l&nbsp;-g&nbsp;-Mavr_enhanced&nbsp;&nbsp;C:\libtest\Test.c

&nbsp;&nbsp;&nbsp;&nbsp;iccavr&nbsp;-o&nbsp;Test&nbsp;-LC:\icc\lib\&nbsp;-g&nbsp;-ucrtatmega.o&nbsp;-bfunc_lit:0x8c.0x20000&nbsp;-dram_end:0x10ff&nbsp;-bdata:0x100.0x10ff&nbsp;-dhwstk_size:16&nbsp;-beeprom:1.4096&nbsp;-fihx_coff&nbsp;-S2&nbsp;@Test.lk&nbsp;&nbsp;&nbsp;-lGetMax&nbsp;-lcatmega

!ERROR&nbsp;file&nbsp;'Test.o':&nbsp;undefined&nbsp;symbol&nbsp;'_GetMax'

C:\icc\bin\imakew.exe:&nbsp;Error&nbsp;code&nbsp;1

Done:&nbsp;there&nbsp;are&nbsp;error(s).&nbsp;Exit&nbsp;code:&nbsp;1

yydgch 发表于 2008-11-1 13:15:55

“执行命令ilibw&nbsp;-a&nbsp;libGetMax.a&nbsp;GetMax.o生成库文件。”这句话是什么意思呀?

fjhcpu 发表于 2008-12-13 21:44:10

我和15楼出现的是同一个问题。。奇怪?

zhy9934 发表于 2008-12-13 22:51:14

mark&nbsp;有空学习

Small 发表于 2008-12-13 22:55:55

mark

mega128 发表于 2008-12-14 19:26:56

mark

letyoufly 发表于 2009-1-22 14:17:20

mark

darlin 发表于 2009-6-12 09:23:58

我也得顶一下~~~又长见识了……

snoopyzz 发表于 2009-6-12 09:42:01

.a在GCC中是rar格式,解开后是lib,是编译过的文件,在链接时才加入编译过程,
.a不提供源代码,一个库至少会提供.a和.h,.h用来声明外部变量和函数...


另外,BS一下挖尸的...

tomjark 发表于 2009-10-7 19:31:21

mark

psocfans 发表于 2009-10-8 08:15:19

mark

xielongyu 发表于 2010-3-17 14:43:35

我也是这个问题
C:\iccv7avr\bin\imakew -f TEST.mak
    iccavr -c -IC:\iccv7avr\include -IE:\AVR\CH375LIB\AVR\FILELIBB\EXAM1 -IC:\icc\include -e -D__ICC_VERSION=722 -DATMega128-l -g -MLongJump -MHasMul -MEnhanced -Wf-use_elpmtext.c
    iccavr -o TEST -LC:\iccv7avr\lib -LC:\icc\lib -g -e:0x20000 -ucrtatmega.o -bfunc_lit:0x8c.0x20000 -dram_end:0x10ff -bdata:0x100.0x10ff -dhwstk_size:16 -beeprom:0.4096 -fihx_coff -S2 @TEST.lkE:\AVR\ww\ff\libGetMax.a   -lGetMax -lcatm128
!ERROR file 'text.o': undefined symbol '_GetMax'
C:\iccv7avr\bin\imakew.exe: Error code 1
Done: there are error(s). Exit code: 1. Wed Mar 17 14:33:49 2010

xielongyu 发表于 2010-3-17 14:44:54

请教各位,希望有位高手能帮忙

C:\iccv7avr\bin\imakew -f HOST.mak
    iccavr -o HOST -LC:\iccv7avr\lib -LE:\AVR\ww\TFT-G2~1.030 -g -e:0x20000 -ucrtatmega.o -bfunc_lit:0x8c.0x20000 -dram_end:0x10ff -bdata:0x100.0x10ff -dhwstk_size:16 -beeprom:0.4096 -fihx_coff -S2 @HOST.lkE:\AVR\ww\TFT-G2~1.030\CH375HFB.A   -lcatm128
!ERROR file 'CH375MAI.o': undefined symbol 'push_gset1'
!ERROR file 'CH375MAI.o': undefined symbol 'push_gset2'
!ERROR file 'CH375MAI.o': undefined symbol 'push_gset3'
!ERROR file 'CH375MAI.o': undefined symbol 'push_gset4'
!ERROR file 'CH375SEC.o': undefined symbol 'push_gset5'
!ERROR file 'CH375MAI.o': undefined symbol 'pop_gset1'
!ERROR file 'CH375MAI.o': undefined symbol 'pop_gset2'
!ERROR file 'CH375MAI.o': undefined symbol 'pop_gset3'
!ERROR file 'CH375MAI.o': undefined symbol 'pop_gset4'
!ERROR file 'CH375SEC.o': undefined symbol 'pop_gset5'
C:\iccv7avr\bin\imakew.exe: Error code 1
Done: there are error(s). Exit code: 1. Wed Mar 17 14:44:26 2010

stud1 发表于 2010-5-8 11:20:41

向fatboy同志致敬!

MO51 发表于 2011-10-18 16:50:43

不错的资料!学习了!

wshy2010 发表于 2011-11-8 10:41:02

mark

ptz19861212 发表于 2012-1-30 15:06:04

MARK

bingshuihuo888 发表于 2012-10-25 10:19:54

mark!!!!!!!!!!!!

SevenWans 发表于 2013-3-25 22:06:50

挖尸~{:biggrin:}{:biggrin:}
页: [1]
查看完整版本: ICCAVR中如何使用自己的库文件?