问题
Let's say I have library (A)
implementing the singleton pattern (it has a static variable in its implementation).
(A)
library is compiled as a static library.
Now, let's say I have in my probject:
(B)
, another static library linking statically with(A)
.(C)
, another static library linking statically with(A)
.(D)
, a top level program linking with(B)
and(C)
.
In the end, is my singleton really a singleton (and my variable really static)? Are (B)
and (C)
seing the same static variable from (A)
(is it unic)? Or does the fact that (A)
was statically linked twice embedded (A)
's code twice ending up with my static variable from (A)
appearing twice in the final binary code? Then if (B)
modifies the static variable value, (C)
would not see the change?
Note: I experienced that when changing the libraries of project to be linked statically instead of dynamically. I'm just wondering if I did something wrong, or if that's a normal known behaviour.
回答1:
First of all:
(B) and (C) do NOT link against (A). Static libs are compiled, not linked. When building (B) and (C) the compiler might need to see certain definitions from (A) but do not confuse this with linking. (A's) code is not copied into (B) or (C).
Secondly:
(D) will have to link against (A), (B) and (C). That means you get only one copy of (A's) code in (D).
Dynamic-link Library/Shared Object:
This of course would be different if (B) and (C) were dlls/sos instead. Dlls are linked and so if you build (B) and (C) as dlls and link them against (A) then you would have a separate copy of (A's) code in both (B) and (C).
Are (B) and (C) seing the same static variable from (A)
This depends on if your variable has external or internal linkage. The following header file contains a static int variable with interal linkage. This means that every translation unit that includes this file will get it's own copy of myVariable
.
//MyHeader.h
#pragma once
static int myVariable = 0;
回答2:
static linked library, and static variables are not related.
A static variable, is not visible outside the current compilation scope (no symbol name is created in the .o file, so no other .o files can find that variable via the symbol name).
This means that the variable
static int foo; can exist in every compilation scope, and each one will be uniqe
回答3:
Your static variable is really static. I suspect that even if (B) links (A), (B) does not pick up it's own copy of (A) - instead it contains information that (A) should be linked.
It's however possible to compile same source code with static variable in two libraries - for example:
test.cpp:
int g_myGlobal = 23;
and compile it in (A) and (B) static libraries, but then when linking whole application or dll - you will get linker error about double defined static variable.
If you however declare variable with static keyword:
test.cpp:
static int g_myGlobal = 23;
Then if same source code is linked from (A) and (B) - it will be compiled and linked ok, but as a result you will have two instances of g_myGlobal variable (which can be even different).
来源:https://stackoverflow.com/questions/36909035/what-happens-to-static-variables-when-libraries-are-statically-linked