当前位置:网站首页>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
边栏推荐
- 下一个行业风口:NFT 数字藏品,是机遇还是泡沫?
- Pelosi: Congress will soon have legislation against members' stock speculation
- Overview of OCR character recognition methods
- Schnuka: visual positioning system working principle of visual positioning system
- ESBuild & SWC浅谈: 新一代构建工具
- Pointer for in-depth analysis (problem solution)
- The solution of permission denied (750 permissions should be used with caution)
- Introduction to DeNO
- Precautions for single chip microcomputer anti reverse connection circuit
- 【Qt5】Qt QWidget立刻出现并消失
猜你喜欢
Canvas cut blocks game code
Analyze menu analysis
Research on cooperative control of industrial robots
深入刨析的指针(题解)
Schnuka: 3D vision detection application industry machine vision 3D detection
2、GPIO相关操作
Explore pointers and pointer types in depth
Overview of OCR character recognition methods
[Li Kou] the second set of the 280 Li Kou weekly match
MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置
随机推荐
Audio audiorecord binder communication mechanism
Brush questions in summer -day3
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
记录一下逆向任务管理器的过程
深度解析指针与数组笔试题
Analyze 菜单分析
八道超经典指针面试题(三千字详解)
Yyds dry inventory what is test driven development
指针笔试题~走近大厂
遥感图像超分辨率论文推荐
Pelosi: Congress will soon have legislation against members' stock speculation
resulttype和resultmap的区别和应用场景
The next industry outlet: NFT digital collection, is it an opportunity or a foam?
【Rust 笔记】18-宏
[slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
SD卡報錯“error -110 whilst initialising SD card
EDCircles: A real-time circle detector with a false detection control 翻译
svg拖动点裁剪图片js特效
Redo file corruption repair