1. why gcc can link objs with multiple same symbols after ar? what's the difference comparing with linking before ar?
2. If multiple same symbols are allowable in linking, then what's the rule to resolve undefined function call(claimed with "extern" before)?
a.c:
extern
int
f();
int main()
... {
printf("result is %d ",f());
return f();
}
int f()
... {
return 1;
}
int main()
... {
printf("result is %d ",f());
return f();
}
int f()
... {
return 1;
}
b.c:
int
f()
... {
return 0;
}
... {
return 0;
}
test1:
>gcc -c a.c -o a.o
>nm -s a.o
00000030 T f
00000000 t gcc2_compiled.
00000000 T main
U printf
00000000 t gcc2_compiled.
00000000 T main
U printf
>gcc -c b.c -o b.o
>nm -s b.o
00000000 T f
00000000 t gcc2_compiled.
00000000 t gcc2_compiled.
>gcc b.o a.o -o main //link failed because of the same global symbols "f"
a.o: In function `f':
a.o(.text+0x30): multiple definition of `f'
b.o(.text+0x0): first defined here
collect2: ld returned 1 exit status
a.o(.text+0x30): multiple definition of `f'
b.o(.text+0x0): first defined here
collect2: ld returned 1 exit status
test2:
>ar cr b.a b.o
f in b.o
b.o:
00000000 T f
00000000 t gcc2_compiled.
00000000 T f
00000000 t gcc2_compiled.
>ar cr a.a a.o
Archive index:
main in a.o
f in a.o
main in a.o
f in a.o
a.o:
00000030 T f
00000000 t gcc2_compiled.
00000000 T main
U printf
00000030 T f
00000000 t gcc2_compiled.
00000000 T main
U printf
>gcc b.a a.a -o main //
link is sucessful!!! what's the difference comparing with test1???
> main
> main
result is 1 //
why is 1???
--------------------------------------------------------------------------------------------------------------------------------------------
a1.c:
extern
int
f();
int main()
... {
printf("result is %d ",f());
return f();
}
int main()
... {
printf("result is %d ",f());
return f();
}
a2.c:
int
f()
... {
return 1;
}
... {
return 1;
}
b.c:
int
f()
... {
return 0;
}
... {
return 0;
}
> gcc -c a1.c -o a1.o
U f
00000000 t gcc2_compiled.
00000000 T main
U printf
00000000 t gcc2_compiled.
00000000 T main
U printf
> gcc -c a2.c -o a2.o
00000000 T f
00000000 t gcc2_compiled.
00000000 t gcc2_compiled.
> gcc -c b.c -o b.o
00000000 T f
00000000 t gcc2_compiled.
> ar cr a2.a a2.o
00000000 t gcc2_compiled.
> ar cr a2.a a2.o
> nm -s a2.a
Archive index:
f in a2.o
f in a2.o
a2.o:
00000000 T f
00000000 t gcc2_compiled.
> ar cr b.a b.o
00000000 T f
00000000 t gcc2_compiled.
> ar cr b.a b.o
> nm -s b.a
Archive index:
f in b.o
f in b.o
b.o:
00000000 T f
00000000 t gcc2_compiled.
00000000 T f
00000000 t gcc2_compiled.
> ar cr a1.a a1.o
> nm -s a1.a
Archive index:
main in a1.o
main in a1.o
a1.o:
U f
00000000 t gcc2_compiled.
00000000 T main
U printf
U f
00000000 t gcc2_compiled.
00000000 T main
U printf
> ar cr a.a a2.o a1.o
> nm -s a.a
Archive index:
f in a2.o
main in a1.o
f in a2.o
main in a1.o
a2.o:
00000000 T f
00000000 t gcc2_compiled.
00000000 T f
00000000 t gcc2_compiled.
a1.o:
U f
00000000 t gcc2_compiled.
00000000 T main
U printf
U f
00000000 t gcc2_compiled.
00000000 T main
U printf
test1:
> gcc b.a a.a -o main
> main
result is 1
test2:
> gcc a1.a b.a a2.a -o main0
> gcc a2.a a1.a b.a -o main0
> main0
result is 0 //
the result depends on linking sequence!!!
> gcc a1.a a2.a b.a -o main1
> gcc b.a a1.a a2.a -o main1
> main1
result is 1 //
the result depends on linking sequence!!!
test3:
> gcc b.a a2.a a1.a -main //
why failed???
a1.a(a1.o): In function `main':
a1.o(.text+0xd): undefined reference to `f'
a1.o(.text+0x25): undefined reference to `f'
collect2: ld returned 1 exit status
a1.o(.text+0xd): undefined reference to `f'
a1.o(.text+0x25): undefined reference to `f'
collect2: ld returned 1 exit status