当前位置:网站首页>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
边栏推荐
- 深度解析指针与数组笔试题
- [slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
- SAP ALV cell level set color
- canvas切积木小游戏代码
- 蓝色样式商城网站页脚代码
- Canvas cut blocks game code
- Eight super classic pointer interview questions (3000 words in detail)
- [padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
- 2.2 STM32 GPIO操作
- 【Rust 笔记】18-宏
猜你喜欢

蓝色样式商城网站页脚代码

Explore pointers and pointer types in depth

Research on cooperative control of industrial robots

Overview of super-resolution reconstruction of remote sensing images

Safety science to | travel, you must read a guide

Overview of OCR character recognition methods

指针笔试题~走近大厂

Eight super classic pointer interview questions (3000 words in detail)

Quartz misfire missed and compensated execution

深入探究指针及指针类型
随机推荐
Safety science to | travel, you must read a guide
八道超经典指针面试题(三千字详解)
遥感图像超分辨重建综述
【Rust 笔记】18-宏
IPv6 comprehensive experiment
Quartz misfire missed and compensated execution
1. New project
蓝色样式商城网站页脚代码
Pointer written test questions ~ approaching Dachang
These are not very good
Shell 传递参数
Arabellacpc 2019 (supplementary question)
js凡客banner轮播图js特效
Differences and application scenarios between resulttype and resultmap
Distributed service framework dobbo
2.2 STM32 GPIO操作
2.2 STM32 GPIO operation
BUAA喜鹊筑巢
Buuctf question brushing notes - [geek challenge 2019] easysql 1
Redis cache breakdown, cache penetration, cache avalanche