一个编译告警可能导致的严重错误

本文详细介绍了编译过程中链接器如何处理重复的全局符号,特别是强符号和弱符号的区别。当遇到多个强符号或多个弱符号时,链接器遵循特定规则选择。如果忽视编译警告,例如关于符号对齐的警告,可能会导致微妙且严重的运行时错误,例如不同类型的重复定义导致的数据覆盖。为了避免此类问题,应重视并理解编译器的警告,或者使用特定的编译选项将警告视为错误。
摘要由CSDN通过智能技术生成
1 /* foo5.c */
2 #include <stdio.h>
3 void f(void);
4
5 int y = 15212;
6 int x = 15213;
7
8 int main()
9 {
10 f();
11 printf(″x = 0x%x y = 0x%x n″,
12 x, y);
13 return 0;
14 }

 

1 /* bar5.c */
2 double x;
3
4 void f()
5 {
6 x = -0.0;
7 }

以上两个文件用以下命令编译时:

gcc -Wall -Og -o foobar5 foo5.c bar5.c

只有一个简单的告警:/usr/bin/ld: warning: alignment 4 of symbol `x' in /tmp/ccYmFWmR.o is smaller than 8 in /tmp/ccIuZptR.o

但是其实埋藏了很严重的问题。直接上书的的原文:

 

7.6.1 How Linkers Resolve Duplicate Symbol Names
The input to the linker is a collection of relocatable object modules. Each of these modules defines a set of symbols, some of which are local (visible only to the module that defines it), and some of which are global (visible to other modules). What happens if multiple modules define global symbols with the same name? Here is the approach that Linux co

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值