Why is global_variable defined as static?

Hi,

I am a bit confused why global_variable is defined as static. Should it not have an empty definition since static makes the variable local to the translation unit?

And also should it not only be used for variables that are defined as extern in other translation units (global_variable is used alot of places where I wonder if it should be local_variable in stead)?
On Handmade Hero, we do not use multiple translation units.

- Casey
Is handmade_optimized.cpp not a different translation unit than handmade.cpp that are linked together?

My question is about global_variable (static) that as I understand makes the variable local not global?

Edited by Christian H. on
Well, I had hoped that the answer would be clear enough, but I will be more precise:

You only need to use "extern" if you are trying to share a variable across multiple translation units. In Handmade Hero, we have three total translation units: one for the platform layer, one for the game, and one for the few specific routines where we need to keep optimizations on all the time.

The only "real" translation unit here is the game, and that's where all the code lives. The platform layer and the optimized translation unit are "special", in that they have consequences - for example, we can not simply share a global variable across the DLL boundary, we have to think carefully about it and make sure we do it in a specific way. We cannot simply declare it "extern" and have it work.

So for our purposes, global variables are strictly those which are global to either the platform layer or the game, and those can (and should) be thus marked "static" since they never cross the translation unit boundary. Any variable which is _actually_ global to the _entire_ program must be treated specially, and we don't actually have any of these other than the debug services, which are handled specially.

- Casey
Hi Casey,

Thank you for your answer. Now I understand what you are saying :)

Chris

Edited by Christian H. on