why gcc can link objs with multiple same symbols after ar?
来源:互联网 发布:js 对话框 确定 取消 编辑:程序博客网 时间:2024/06/09 11:01
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
- why gcc can link objs with multiple same symbols after ar?
- multiple lua files with same-named functions
- Multiple charts with same series color
- Multiple params with html:link tag
- Installing multiple CAB files with the same setup application
- android studio f4出现Multiple entries with same key
- Why can be so about Regex.IsMatch with double backslash?
- why 'not in' can not be followed with 'null'
- .JBOSS 4.3.0 EAP Clustering with multiple instances running on Same machine balanced with Apache HTT
- Can I use multiple versions of jQuery on the same page?
- kaldi can link with openfst-1.5.0 without patch
- users can not login after click on activation link. VM and CB
- Why Link Prediction?
- Install multiple instances of Reporting Services on the same computer (for use with Microsoft Dynami
- Why is VS development not supported with multiple versions of Office?
- Why is VS development not supported with multiple versions of Office
- GCC Link
- Why can't MEX find a supported compiler in MATLAB R2015b after I upgraded to Xcode 7.0?
- 从网络上找到的一些关于软件工程的东西
- 如何拯救将被淘汰的电脑机房?ltsp/FreeNX
- J2SE过程中的30个基本概念
- ltsp 指南
- JS关键字
- why gcc can link objs with multiple same symbols after ar?
- NoMachine, NX远程遥控的新典范(zz chinalab)
- Christmas Songs
- 开篇:我java方向的第一次笔试
- NX和FreeNX
- showModalDialog 和 showModelessDialog
- 男人,请把哄的时间延长些
- 战报:KB929391正版验证补丁已经被破解
- JavaScript中两个日期相减