当前位置:网站首页>Redefinition of global variables in C language in multiple files
Redefinition of global variables in C language in multiple files
2022-04-21 19:34:00 【WoLannnnn】
First, let's take a look at the following two codes :
test.c in :

test1.c in :

At this point, we compile and run :

Compile and pass , And printed out 10, Why is that ? This involves the linker resolving multiple defined global symbols .
Linux Rules for linkers to handle multiple defined symbols
The above result is that we are VS2019 Under test , Linux The rule for the linker to handle multiple defined symbols is :
- Multiple with the same name are not allowed Strong sign
- If there is a Strong sign And multiple Weak sign The same name , choice Strong sign
- If there are more than one Weak sign The same name , From these Weak sign Choose one randomly
First , What is a strong symbol , What is a weak sign ?
Strong sign : Functions and initialized global variables are strong symbols
Weak sign : Uninitialized global variables are weak symbols
In the above code ,test.c Medium count It's a strong sign ,test1.c Medium count Is a weak sign , When printing, strong symbols are printed count, so , stay Windows This rule is also followed under
stay Linux and Windows Next test
We can do it in Windows and Linux Let's test these three rules :
When multiple source files have strong symbols with the same name
When multiple source files have strong symbols with the same name :
Linux:

Compile link results :

Windows:

thus it can be seen , stay Linux and Windows Multiple strong symbols with the same name are not supported under
When multiple source files have a strong symbol with the same name as multiple weak symbols
When there is a strong symbol with the same name as multiple weak symbols in multiple source files :
Linux:

Compile and run :

Windows:
Like the example shown at the beginning , Compile and print 10
so , When there is a strong symbol with the same name as multiple weak symbols in multiple source files ,Linux and Windows The strong symbol will be selected under
When there are multiple weak symbols with the same name
When there are multiple weak symbols with the same name :
Linux:

Running results :

Windows:

Running results :

so , Allow multiple weak symbols to duplicate names , And will randomly choose a weak symbol to use .
Why print 0? Because uninitialized global variables are assigned when linking 0 value
版权声明
本文为[WoLannnnn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211927411194.html
边栏推荐
- detectron2 中yacs的使用解读
- 108. Convert an ordered array into a binary search tree (picture and text merging)
- 调试 ms 源代码
- 状态码封装--转载
- MySQL MHA high availability cluster deployment and failover
- Rapid generation of latex from excel tables
- Overview of relational database and non relational database, introduction to redis, common commands and optimization
- 山东大学项目实训(四)多个点标记添加点击事件
- Introduction to GPS Beidou satellite time synchronization system (clock device) technology of power grid
- Daily CISSP certification common mistakes (April 20, 2022)
猜你喜欢

全职加入清华,丘成桐:为祖国、为全球数学界培养数学人才

Redis基础

MySQL数据库索引面试题(最新版)

Use the mremoting tool to manage all remote connections

What are the factors affecting VPS website optimization?

HMS Core 6.4.0版本发布公告

Switch branch

The CPU and memory consumption are too high, but the memory and CPU occupied by the processes in the task manager cannot be seen

Automatic control principle Chapter 6 - correction and synthesis of control system (mind map)

Sleuth + Zipkin link tracking
随机推荐
Abbexa MPO (FITC) / CD3 (PE) combined antibody
【手部姿态估计】【论文解读】3D Human Pose Estimation in RGBD Images for Robotic Task Learning
MusicPlayer2.1版本
669. Pruning binary search tree
Webrtc video cannot be played. How to add UDP hole drilling program in easycvr?
Rapid generation of latex from excel tables
Edgeboard records
Instruction of crystal Chem active GIP ELISA Kit
Analysis of ribbon principle and Nacos service discovery principle
Alternative JC-1, mito-id series mitochondrial membrane potential detection kit
Relationship between deep learning, multi machine and multi card batchsize and learning rate
知识点合集
Engineering method for improving model speed / accuracy
switch分支
数据分析之数据预处理
leetcode344. 反转字符串
学完这篇Charles抓包教程,我直接把fiddler卸载了
[leetcode] daily question: goat Latin
MySQL MHA高可用集群部署及故障切换
mysql (三) 索引优化以及案例分析