initializers is based on the priorities that are specified in the source code, or at compile time. If no priority is specified, the default priority settings are applied. The following example illustrates the ordering of initializers based on the priority specified at
$ cat main.C #include <stdio.h>
struct A {
A() {printf("Constructing A\n");}
}; A a;
//This will be 1st initializer entry in a.out
//Since init routines are execute in reverse order
//it will be executed after A()
#pragma priority
B() {printf("Constructing B\n");}
}; B b;
int main() { printf("In main\n");
}
$ aCC main.C $ a.out Constructing A Constructing B In main
Ordering Among Executables and Shared Libraries
If multiple load modules have initializers/terminators, the following rules apply to ordering:
•While loading, the inits and
•While unloading, the finis and
•If a shared library is itself a dependent of one of its dependents (a "circular" dependency), no ordering between them is guaranteed.
For example, given three libraries: libA.so, libB.so, libC.so. If libA.so were linked as (libB.so and libC.so are "dependent" libraries of libA.so):
$ ld
One possible ordering while loading is:
•inits in C
•inits in B
•inits in A
and while unloading is:
•finis in A
•finis in B
•finis in C
142 Shared Library Management Routines