daltonfury42 daltonfury42 - 5 months ago 47
Linux Question

Why are global variables static in the linux kernel?

I was reading TLDP and noticed that all global variables and functions are declared static. After further reading, I understand that the variables are declared static to reduce namespace pollution.

According to this SO post,

static functions are functions that are only visible to other
functions in the same file (more precisely the same translation unit).

Hence declaring functions as static will reduce namespace pollution. But, in the case of variables, according to TLDP:

When a Static variable is modified by a module, all other modules will
see the new value.

it would increase namespace pollution. Aren't global static variables also visible to the same translation unit? If so how the above quoted statement true? I seem to be missing something.


When a Static variable is modified by a module

I believe you are misinterpreting that sentence. It can be a bit confusing. It is not referring to the C static keyword. It is referring to the C standard's usage of "static storage duration". Refer to section 6.2.4 of the C standard:

1 An object has a storage duration that determines its lifetime. There are three storage durations: static, automatic, and allocated.


3 An object whose identifier is declared with external or internal linkage, or with the storage-class specifier static has static storage duration. Its lifetime is the entire execution of the program

That is, "static storage duration" variables include both global variables and variables declared with the C static keyword. In the TLDP article it is referring to the former.