Order of object initialization across libraries

At run time, once all modules in an application have been loaded, the modules are initialized in their order of priority (the executable program containing the main function is always assigned a priority of 0). When objects are initialized within a library, the order of initialization follows the rules outlined in Assigning priorities to objects. If objects do not have priorities assigned, or have the same priorities, object files are initialized in random order, and the objects within the files are initialized according to their declaration order. Objects are terminated in reverse order of their construction.

For objects assigned the same priorities, if you want to control their initialization order, you can use the -Wm option to do so. -Wm with the -c suboption specifies that object files with the same priority are to be initialized in link order — where link order is the order in which the files were given on the command line during linking into the library — and the static objects within the files are initialized according to their declaration order. -Wm with the -r suboption specifies that the object files with the same priority are to be initialized in reverse link order.

Example of object initialization across libraries

In this example, the following modules are used:
  • main.out, the executable containing the main function
  • libS1 and libS2, two shared libraries
  • libS3 and libS4, two shared libraries that are dependencies of libS1
  • libS5 and libS6, two shared libraries that are dependencies of libS2
The source files are compiled into object files with the following command strings:
xlC -qpriority=101 -c fileA.C -o fileA.o
xlC -qpriority=150 -c fileB.C -o fileB.o
xlC -c fileC.C -o fileC.o
xlC -c fileD.C -o fileD.o
xlC -c fileE.C -o fileE.o
xlC -c fileF.C -o fileF.o
xlC -qpriority=300 -c fileG.C -o fileG.o
xlC -qpriority=200 -c fileH.C -o fileH.o
xlC -qpriority=500 -c fileI.C -o fileI.o
xlC -c fileJ.C -o fileJ.o
xlC -c fileK.C -o fileK.o
xlC -qpriority=600 -c fileL.C -o fileL.o
The dependent libraries are created with the following command strings:
xlC -qmkshrobj=50 -o libS3.a fileE.o fileF.o 
xlC -qmkshrobj=-600 -o libS4.a fileG.o fileH.o
xlC -qmkshrobj=-200 -o libS5.a fileI.o fileJ.o
xlC -qmkshrobj=-150 -o libS6.a fileK.o fileL.o
The dependent libraries are linked with their parent libraries using the following command strings:
xlC -qmkshrobj=-300 -o libS1.a fileA.o fileB.o -L. -lS3 -lS4
xlC -qmkshrobj=100 -o libS2.a fileC.o fileD.o -L. -lS5 -lS6
The parent libraries are linked with the main program with the following command string:
xlC main.C -o main.out -L. -R. -lS1 -lS2

The following diagram shows the initialization order of the objects in the shared libraries.

Figure 1. Object initialization order on AIX®
Object initialization order on AIX
First, the shared libraries are initialized, in the following order:
Sequence Object Priority value Comment
1 libS4 -600 Initialized first (lowest priority number).
2 libS1 -300 Initialized next (next priority number).
3 libS5 -200 Initialized next (next priority number).
4 libS6 -150 Initialized next (next priority number).
5 main.out 0 Initialized next (next priority number). The main program always has a priority of 0.
6 libS3 50 Initialized next (next priority number).
7 libS2 100 Initialized last (next priority number).
Then, each of the files is initialized, in the following order:
Sequence Object Priority value Comment
8 fileH 200 Initialized first (contained in libS4; lowest priority number).
9 fileG 300 Initialized next (contained in libS4; next priority number).
10 fileA 101 Initialized next (contained in libS1; lowest priority number).
11 fileB 150 Initialized next (contained in libS1; next priority number).
12 fileJ 0 Initialized next (contained in libS5; lowest priority number).
13 fileI 500 Initialized next (contained in libS5; next priority number).
14 fileK 0 Initialized next (contained in libS6; lowest priority number).
15 fileL 600 Initialized next (contained in libS6; next priority number).
16     Objects in main are initialized according to their priority.
17 fileE, fileF 0, 0 Initialized next, in random order (contained in libS3; same priority number).
18 fileC, fileD 0, 0 Initialized next, in random order (contained in libS2; same priority number).