GNU offers some non-standard features in their C and C++ compilers. They call these extensions. I do not recommend using extensions where possible, they are extremely compiler specific and make your code less portable.
That said, they are still sometimes the best tool for the job (e.g. you can’t do something any other way, or the non-compiler specific method is ugly and horrible).
GNU allows you to determine the initialisation order of global static variables by specifying a priority.
// In some .cpp file...
MyClass myVar1 __attribute__ ((init_priority (321)));
MyClass myVar2 __attribute__ ((init_priority (12)));
Usually, myVar1 would be initialisaed before myVar2. However, with myVar2 being specified with the priority 12 (lower numbers equate to a higher priority), myVar2 will be initialised first.
Posted: August 15th, 2014 at 4:19 pm
Last Updated on: October 7th, 2015 at 3:15 pm