LinuxSir.cn,穿越时空的Linuxsir!

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

请教一个关于程序链接的问题

[复制链接]
发表于 2005-6-2 19:58:28 | 显示全部楼层 |阅读模式
#ifndef TEST1_H
#define TEST1_H
extern int Add(int,int);
#endif

//test1.c
#include "test1.h"
int Add(int a,int b){
        return a+b;
}

//main.c
#include "test1.h"
#include <stdio.h>
main(){
        int c = Add(3,5);
        printf("result");
}

使用下面命令
gcc -c test1.c
gcc -c main.c
ar cru libtest.a test1.o
gcc -o test1 libtest.a main.o
总是报链接错误,但是最后一行换成
gcc -o test1 main.o libtest.a
就没问题。
请问各位大侠这是什么原因?
另外只用.o文件就不存在这种现象,gcc -o test1 main.o test1.o 和
gcc -o test1 test1.o main.o都ok
发表于 2005-6-2 20:00:36 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2005-6-2 20:02:11 | 显示全部楼层
发现好多人对库的问题感兴趣啊
回复 支持 反对

使用道具 举报

发表于 2005-6-2 20:23:23 | 显示全部楼层
巧合巧合 呵呵~~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-6-2 20:36:23 | 显示全部楼层
大侠推荐的那篇文章还是没能解决我心中的疑问,为什么libtest.a非要放到后面不可,在这里libtest.a其实不就是test.o打个包吗
回复 支持 反对

使用道具 举报

发表于 2005-6-2 20:46:47 | 显示全部楼层
你的用法错了
具体的方法我觉得我讲得蛮清楚了啊
回复 支持 反对

使用道具 举报

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

本版积分规则

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