当前位置:网站首页>A brief introduction to symbols and link libraries in C language
A brief introduction to symbols and link libraries in C language
2022-07-06 03:29:00 【It's Ah Xiang】
sketch C Symbols and link libraries in languages
Strong and weak symbols and their rules
Linker in parsing symbolic references , Associate a certain symbol definition in the symbol table of the relocatable target file he entered .
Local variables and static local variables have only one definition .
But it will be much more complicated to get reference resolution for global symbols , When the compiler encounters symbols that are not defined in this module , Assume that it is defined in some other module , At this time, a linker symbol will be generated and sent to the linker for processing , If the linker cannot be found, it will report an error and exit
However, there will be global symbols with the same definition in multiple target files , At this point, you want the assembler to output each global symbol , Or strong sign Or weak sign .
According to the definition of strong and weak symbols , linux The linker uses the following rules to handle multiple defined symbolic names
- The rules 1: Multiple strong symbols with the same name are not allowed
- The rules 2: If there is a strong sign and multiple weak signs Choose a strong symbol
- The rules 3: If there are multiple weak symbols with the same name , Will choose any one of these weak symbols
notes : Weak symbols are usually global declaration variables Assign values locally
The rules 2 It will cause the global variables defined by other modules to be modified by the local symbol reference of this module
The rules 3 So it is with
What's more terrible is when the repeated symbol definitions are still different types , It will cause other data to be overwritten
For example, the strong symbols defined by other modules are int x This module declares double x
because double The occupied bytes are int Of 2 times In the process of assignment, other x Modify the value of the nearby memory address , Cause an unknown error in the program .
When we suspect that such mistakes will occur It can be used GCC-fno-common Option call linker This option will tell the linker that an error will be triggered when it encounters multiple defined global symbols … You can also use -Werror Turn all warnings into errors
COMMON There are no entries in the section header table , Is a special pseudo section (oseudosection), Represents an uninitialized data target that has not been assigned a location .
COMMON For initialized global variables
.bss Uninitialized static variables , And initialize to 0 The global or static variables of
The rules : When the compiler is translating a module , Encountered a weak global symbol , Because he doesn't know whether it is defined in other modules , So the compiler will put it in COMMON paragraph , The decision is left to the linker . On the other hand If you initialize it to 0 Then he is a strong symbol , So the compiler can confidently assign it to .bss paragraph ,
Allied Because the construction of static symbols is unique So the compiler can confidently divide them into .data perhaps .bss
Link to static library
We can package all commonly used object modules into a single file , Call it a static library , Like the ones that we see all the time libc.a There is also support for mathematical functions labm.a Static library, etc
Advantages and disadvantages of static library :
If we don't use static libraries Every time a lot of files are compiled , Although we will be more convenient to use Because standard functions are always available ( All have the source code )
But the bad thing is that it will greatly increase the burden of the compiler , As a result, the compilation time becomes very long
If we use static libraries , You can separate the implementation of compilers from the implementation of standard functions , And still maintain moderate convenience for programmers . However, the disadvantage is that each executable file in the system must contain a complete copy of the standard function set , This disk is a great waste . What's worse is , Each running program will put their copies of these functions in memory , This is a great waste of memory . Another drawback is , Changes to any standard library functions , No matter how small the changes are, the entire source file must be recompiled , It is a very time-consuming operation
Of course, you can also create a separate relocatable file for each standard function , But this method requires the programmer to explicitly link the appropriate module to the executable , This process is very error prone and time-consuming There is a pile behind .o
In order to solve these shortcomings , We can compile relevant functions into independent object files . Then package these object files into .a file
In this way, only the modules referenced by the program will be copied when linking , This reduces the consumption of disk and memory . On the other hand, we only need to include less .a File can
-lpthread Namely route /pthread.a Abbreviation
The order of links is important , Incorrect sequence will report an error
So usually put the library file at the end
边栏推荐
- Audio audiorecord binder communication mechanism
- Distributed service framework dobbo
- Cross origin cross domain request
- February 14, 2022 Daily: Google long article summarizes the experience of building four generations of TPU
- 深入刨析的指针(题解)
- 3.2 rtthread 串口设备(V2)详解
- mysqldump数据备份
- Performance analysis of user login TPS low and CPU full
- Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
- 【SLAM】lidar-camera外参标定(港大MarsLab)无需二维码标定板
猜你喜欢
施努卡:视觉定位系统 视觉定位系统的工作原理
The next industry outlet: NFT digital collection, is it an opportunity or a foam?
MPLS experiment
给新人工程师组员的建议
深入探究指针及指针类型
适合程序员学习的国外网站推荐
Overview of super-resolution reconstruction of remote sensing images
Map sorts according to the key value (ascending plus descending)
pytorch加载数据
Schnuka: visual positioning system working principle of visual positioning system
随机推荐
Pytorch基础——(2)张量(tensor)的数学运算
pytorch加载数据
遥感图像超分辨率论文推荐
Schnuka: 3D vision detection application industry machine vision 3D detection
JS音乐在线播放插件vsPlayAudio.js
【Rust 笔记】18-宏
Introduction to DeNO
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
[slam] orb-slam3 parsing - track () (3)
Pytorch load data
Redis cache breakdown, cache penetration, cache avalanche
手写数据库客户端
. Net 6 and Net core learning notes: Important issues of net core
1. New project
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
SD卡報錯“error -110 whilst initialising SD card
Shell pass parameters
Polymorphic day02
Lua uses require to load the shared library successfully, but the return is Boolean (always true)
深入刨析的指针(题解)