当前位置:网站首页>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

原网站

版权声明
本文为[It's Ah Xiang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060325243025.html