Chinaunix首页 | 论坛 | 博客

分类: LINUX

2014-03-12 00:25:21

        自己很少写Makefile,更加体会不到libtool的好处。编译代码时经常出现以下两个错误:
        collect2: ld returned 1 exit status, 一般顺着错误往上找,多数是因为缺少库的原因。一般可通过在Makefile文件中LIBS后面增加动态库或静态库解决.
        libtool: link: `xxx.lo' is not a valid libtool object.  这个原因的解决办法一般是删除xxx.lo文件,重新编译。最好是rm *.lo
 
       通过在网上查看例子,稍微清楚了libtool的功能,主要参考的是http://blog.chinaunix.net/uid-20682890-id-3487130.html。函数写的没问题,但是
貌似Makefile文件写的不是很对,自己稍微看看了语法。
       xxx.lo 文件保存了xxx.o文件所在的位置。如下:
# hello.lo - a libtool object file
# Generated by libtool (GNU libtool) 2.4.2 Debian-2.4.2-1ubuntu1
#
# Please DO NOT delete this file!
# It is necessary for linking the library.

# Name of the PIC object.
pic_object='.libs/hello.o'

# Name of the non-PIC object
non_pic_object='hello.o'
       xxx.la 文件记录了动态库或者静态库的一些信息,由这写信息可查找到对应的库(.lo文件的目的也是找到对应的.o文件)。

最后贴上自己的Makefile文件吧。
OBJS = fun.o hello.o
LO_OBJS = fun.lo hello.lo
PACKAGE_VERSION = 1:1:1
all : test
install : libhello.la


test : libhello.la main.o
        libtool --mode=link gcc -g -O -o test main.la libhello.la
libhello.la : $(OBJS)
        libtool --mode=link gcc -g -O -o libhello.la $(LO_OBJS)
main.o : main.c fun.h hello.h
        libtool --mode=compile gcc -g -O -c main.c
fun.o : fun.c fun.h
        libtool --mode=compile gcc -g -O -c fun.c
hello.o : hello.c hello.h
        libtool --mode=compile gcc -g -O -c hello.c
clean :
        @rm -f OBJ/* lib*.a *~ *core *.lo *.o *.la
        @rm -rf .libs

不理解的是,在libtool --mode=link gcc -g -O -o libhello.la $(LO_OBJS)中增加-dynamic参数时,没有生成对应的 libhello.so文件,且原来的.la文件也变成二进制形式了。
以后要是碰着再说吧.

http://blog.chinaunix.net/uid-26310563-id-3025366.html  进阶阅读。


阅读(5161) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~