static variable in static library

We know that static variables in C language have only one copy when the program is running. After changing its value in any part of the program, calling it elsewhere will get the modified value, so it can be used as a global variable to use.

However, when the system is relatively large, some functional codes are often compiled into various libraries to be called. The libraries here include static libraries and dynamic libraries. 

The question is, when a static variable is defined in a static library, does it have only one copy? The answer is not necessarily. Consider such a situation, there is a static library liba, which contains the static variable x, and also There are two dynamic libraries dlib1 and dlib2, both of which contain the static library liba when they are created, so the static variable x has a copy in each of the two dynamic libraries, and the two copies are independent and do not affect each other Yes. If you still write the program according to the idea of ​​only one copy, there will be inexplicable problems. 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325746497&siteId=291194637