When compiling and linking a C++ library or program, there are no guarantees on the order of initialization of static C++ objects among translation units. GCC offers init_priority
to solve the problem for static archives, shared objects and programs:
init_priority (priority)
In Standard C++, objects defined at namespace scope are guaranteed to
be initialized in an order in strict accordance with that of their
definitions in a given translation unit. No guarantee is made for
initializations across translation units. However, GNU C++ allows
users to control the order of initialization of objects defined at
namespace scope with the init_priority attribute by specifying a
relative priority, a constant integral expression currently bounded
between 101 and 65535 inclusive. Lower numbers indicate a higher
priority.
(The static archive seems to be trickiest because it will (presumably) be linked by someone else).
What does Microsoft offer for controlling the order of initialization of static C++ objects among translation units?
#pragma init_seg
is probably the most similar documented equivalent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With