6

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?

1 Answer 1

4

#pragma init_seg is probably the most similar documented equivalent.

1
  • Thanks for the answer. I have not forgot about you. I just have not had time to test it (and accept if it works).
    – jww
    Aug 13, 2015 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.