why gcc can link objs with multiple same symbols after ar?

本文探讨了GCC编译器在不同情况下如何处理具有相同符号的多个目标文件的链接过程。通过具体的代码示例展示了直接链接与使用归档文件时链接器的行为差异,以及链接顺序对最终结果的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 b.c:

int f()
...{
    
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
>gcc -c b.c -o b.o
>nm -s b.o
00000000 T f
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
test2:
>ar cr b.a b.o
f in b.o
b.o:
00000000 T f
00000000 t gcc2_compiled.
>ar cr a.a a.o
Archive index:
main in a.o
f in a.o
a.o:
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
result is 1                                                //why is 1???
 
--------------------------------------------------------------------------------------------------------------------------------------------
a1.c:
extern int f();

int main()
...{
    printf(
"result is %d  ",f());
    
return f();
}


a2.c:
int f()
...{
    
return 1;
}

b.c:

int f()
...{
    
return 0;
}

 
> gcc -c a1.c -o a1.o
                   U f
00000000 t gcc2_compiled.
00000000 T main
                   U printf
> gcc -c a2.c -o a2.o
00000000 T f
00000000 t gcc2_compiled.
> gcc -c b.c -o b.o
00000000 T f
00000000 t gcc2_compiled.
> ar cr a2.a a2.o
> nm -s a2.a
Archive index:
f in a2.o
a2.o:
00000000 T f
00000000 t gcc2_compiled.
> ar cr b.a b.o
> nm -s b.a
Archive index:
f in b.o
b.o:
00000000 T f
00000000 t gcc2_compiled.
> ar cr a1.a a1.o
> nm -s a1.a
Archive index:
main in a1.o
a1.o:
                   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
a2.o:
00000000 T f
00000000 t gcc2_compiled.
a1.o:
                   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
 
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值