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 compile-time:

$ 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 -10 struct B {

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 HP-UX 10.X style initializers of any dependent libraries are called before the ones in the current library.

While unloading, the finis and HP-UX 10.X style initializers of any dependent libraries are called after the finis of the current library.

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 -b foo.o -lB -lC -o libA.so

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