LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1181|回复: 9

静态链接库的问题

[复制链接]
发表于 2005-12-15 11:21:48 | 显示全部楼层 |阅读模式
我现在手头上有一个工程,比较大,于是它把其中几个大的模块分别打成.a的静态库。最后的一步将这些库同主文件等.o文件连接,生成一个可执行文件。
但是我想把最后这一步改一下,让他生成一个.a的库,这样的话在外面编写一个比较简单的程序就可以调用它了。否则这个可执行文件的调用比较麻烦,我现在只会用system函数来调用。
请问怎么才能实现呢?
我看到有个帖子《链接与库的一个问题》,但是还是不太理解。
现在问题出现在所有工作的最后一步,也就是将简单的调用程序同第二个库进行连接时,它说我第一个库中的接口函数undefined。可是我明明是在第一个库中声明了的,extern int func();
这是为什么呢?请说的详细一点,要不我怕我不明白(这方面不熟悉),谢了
 楼主| 发表于 2005-12-15 11:41:39 | 显示全部楼层
我写了一段小程序作为例子
***********************
[root@localhost ar]# gcc -c hellofirst.c hellosecond.c hellos.c
[root@localhost ar]# ar -r libhello.a hellofirst.o hellosecond.o
ar: 正在创建 libhello.a
[root@localhost ar]# ar -r libhellos.a hellos.o libhello.a
ar: 正在创建 libhellos.a
[root@localhost ar]# gcc ovxt.c libhellos.a -o ovxt
libhellos.a(hellos.o)(.text+0x7): In function `hello':
: undefined reference to `hellofirst'
libhellos.a(hellos.o)(.text+0xc): In function `hello':
: undefined reference to `hellosecond'
collect2: ld returned 1 exit status
[root@localhost ar]#


/*hellofirst.c*/
#include <stdio.h>
extern void hellofirst();
void hellofirst()
{
        printf("The first hello\n");
}

/*hellosecond.c*/
#include <stdio.h>
extern void hellosecond();
void hellosecond()
{
        printf("The second hello\n");
}

/*hellos.c*/
extern void hellofirst();
extern void hellosecond();
extern void hello(void)
{
        hellofirst();
        hellosecond();
}

/*ovxt.c*/
extern void hello(void);
int main(int argc, char *argv[])
{
        hello();
        return(0);
}
************************
回复 支持 反对

使用道具 举报

发表于 2005-12-15 12:17:32 | 显示全部楼层
我想你恐怕要这样: ar -r libhellos.a hellofirst.o hellosecond.o hellos.o
ar只是简单的打包,可能ld不支持多层的包
回复 支持 反对

使用道具 举报

发表于 2005-12-15 13:33:23 | 显示全部楼层
Post by likedaxia

[root@localhost ar]# gcc ovxt.c libhellos.a -o ovxt
libhellos.a(hellos.o)(.text+0x7): In function `hello':

用AR打出来的库文件不是这样用的吧!
gcc ovxt.c -L./ -lhellos -o ovxt
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-12-15 14:00:05 | 显示全部楼层
Post by x11
我想你恐怕要这样: ar -r libhellos.a hellofirst.o hellosecond.o hellos.o
ar只是简单的打包,可能ld不支持多层的包
我试了一下,确实可以了,可是这样做好麻烦,尤其是我第一步的.a文件就很多,而每个.a都是由好多.o生成的,这样的话当我进行第二步的时候.o恐怕有上百个了。有没有更好的方法呢?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-12-15 14:01:05 | 显示全部楼层
Post by haohao_h
用AR打出来的库文件不是这样用的吧!
gcc ovxt.c -L./ -lhellos -o ovxt
那样做是可以的,因为就是当前目录,而且是全称。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-12-15 15:16:12 | 显示全部楼层
刚才看了一下,所有用到的.o文件一共500多个,而且有好多名字一样,faint。刚才我试了一下,如果在ar的时候把那些.a文件也写上的话可以编进去的,是从那个生成的.a文件大小看出来的,可就是没法用,比较郁闷。
回复 支持 反对

使用道具 举报

发表于 2005-12-15 18:11:37 | 显示全部楼层
你“第一步”的时候别用ar用gcc就可以了!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-12-15 18:39:33 | 显示全部楼层
你的意思是说第一步别编成.a而只是编成.o,然后第二步的时候一起变成.a,对吧?
我试了一下,总共的.o加起来有500多个,而编成.a后也没法按预想的调用,错误无数,可能是函数定义的错误吧,还没有仔细看。
我觉得这种方法并不可行呀,应该有更好的方法才对。
回复 支持 反对

使用道具 举报

发表于 2005-12-16 09:41:10 | 显示全部楼层
[/usr3/inter/lipeng/study] ar -r libhello.a hellofirst.o hellosecond.o
ar: creating libhello.a
[/usr3/inter/lipeng/study]ar -q libhello.a hellos.o
[/usr3/inter/lipeng/study]cc ovxt.c   libhello.a -o ovxt  
[/usr3/inter/lipeng/study]ovxt
The first hello
The second hello

用-q追加吧  man了一下 就看懂点这个
供大家参考
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表