The Secret Life of C++: Initializing Globals

Global variables are tricky in C++, since they can have constructors and destructors.

Simple Global Objects

Lets look at a simple example of initializing a global object:

        

        

        

        

        

Linker Magic

Basically, global constructors and destructors get "registered" and called at program start and shutdown, using the _init and _fini sections of the executable. Precisely how they get there is linker magic.

TODO: Figure out linker magic.

Fork me on GitHub