I need to enforce a certain initialization and deinitialization order of static variables in GCC, even between different translation units. There is an extension called init_priority, which initializes my variable first, but its deinitialization order is weird:
----------------------------8<------------------------------ #include <stdio.h> struct A { A() { printf("A()\n"); } ~A() { printf("~A()\n"); } }; struct B { B() { printf("B()\n"); } ~B() { printf("~B()\n"); } }; struct C { C() { printf("C()\n"); } ~C() { printf("~C()\n"); } }; struct D { D() { printf("D()\n"); } ~D() { printf("~D()\n"); } }; static A a; static B b; static C c __attribute__((init_priority(101))); static D d; int main(int argc, char *argv[]) { return 0; } ----------------------------8<------------------------------ It produces: C() A() B() D() ~C() <= deinitialization order error ~D() ~B() ~A() But I expect: C() A() B() D() ~D() ~B() ~A() ~C() Why isn't c destroyed at the very end? Is it a bug or a correct behaviour? I don't see anything like attribute(deinit_priority), so how can I obtain the latter deinitialization order (nifty counters etc. are not allowed, because third-party libraries do not include them)? Best reagrds Piotr Wyderski