简体   繁体   中英

How 'extern' variables are handled in dynamically linked libraries?

Recently I changed (for packaging) my own libraries to STATIC. Now i receive error messages that a variable defined in the library is multiply defined. It is OK, the libraries use each other. But why was this not noticed by the linker until I changed to STATIC? In one of my files, I set the variable declared as 'extern' and the linker also flags it as 'multiply defined'. Is it OK?

Basically the compiler has 4 stages:

pre-processing: macro and symbol edition

compiling : generate assembling code to be executed by the processor

assembling : generate binary code that the machine can understand(0/1 binary code)

Linking: the three previous operations are done separately for each file, however we need to edit the address mapping of each variable, pointer, function for the whole project here when we will have some problems when we have some multiple definition of variable because linking will check all file and generate an output for the whole project.

if a library is declared as static then the declared and defined function inside this library can't be used until the run time but in compile time it is not allowed to use this library in other files so if it is the case we will get errors during linking stage because the compiler will figure out the use of the function inside this static library by another file which is not allowed.

If you want it static , then use runtime concept to use this library (for example in C you can use pointer to function .

But why was this not noticed by the linker until I changed to STATIC?

Runtime linker allows duplicate symbol definitions (only one will be used at runtime, this is symbol interposition ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM