[问题] 一个简单的makefile错误

楼主: e12518166339 (耐纶)   2013-03-10 16:23:02
一个简单的makefile的习题,如题,错误讯息如下
a.o:(.rodata+0x0):“a”的多重定义
ab.o:(.rodata+0x0):第一次定义在此
b.o:(.rodata+0x0):“b”的多重定义
ab.o:(.rodata+0x4):第一次定义在此
collect2: 错误: ld 回传 1
make: *** [lab1] Error 1
我的makefile source code如下:
lab1:ab.o a.o b.o
gcc -o lab1 ab.o a.o b.o
ab.o:ab.c a.h b.h
gcc -c ab.c
a.o:a.c a.h
gcc -c a.c
b.o:b.c b.h
gcc -c b.c
clean:
rm -f *.o
不清楚到底是那里出错了,有人可以为我解惑吗?
另外附上相关档案
a.h : const int a=1;
b.h : const int b=2;
a.c: #include<stdio.h>
#include<stdlib.h>
#include"a.h"
void a_fun()
{
printf("a=%d\n",a);
}
b.c: #include<stdio.h>
#include<stdlib.h>
#include"b.h"
void b_fun()
{
printf("b=%d\n",b);
}
ab.c: #include<stdio.h>
#include<stdlib.h>
#include"a.h"
#include"b.h"
void main()
{
printf("a+b=%d\n",(a+b));
}
明明是一个简单的练习,可是我不晓得错在那找了找网络似乎也没人犯过这种错
可能这种错很低能,但是希望各位前辈不吝赐教
感谢大家
作者: Bencrie   2013-03-10 16:59:00
header file 不要指定值,把它移到别的 source file你现在的写法只要有 source 档 include a.h/b.h 就会多一份 a/b 的定义。
作者: alongalone (沿着孤单的路)   2013-03-11 14:38:00
重复include a.h,b.h.你可以去查header file如何避免重复include
作者: johnny1276 (山羊)   2013-03-13 09:10:00
用ifdef?

Links booklink

Contact Us: admin [ a t ] ucptt.com