Each static library and shared library is loaded and initialized at run time in reverse link order, once all of its dependencies have been loaded and initialized. Link order is the order in which each library was listed on the command line during linking into the main application. For example, if library A calls library B, library B is loaded before library A.
As each module is loaded, objects are initialized in order of priority, according to 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 reverse 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 objects within the files are initialized according to their declaration order. Objects are terminated in reverse order of their construction.
In this example, the following modules are used:
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 -o libS3.so fileE.o fileF.o xlC -qmkshrobj -o libS4.so fileG.o fileH.o xlC -qmkshrobj -o libS5.so fileI.o fileJ.o xlC -qmkshrobj -o libS6.so fileK.o fileL.o
The dependent libraries are linked with their parent libraries using the following command strings:
xlC -qmkshrobj -o libS1.so fileA.o fileB.o -L. -R. -lS3 -lS4 xlC -qmkshrobj -o libS2.so fileC.o fileD.o -L. -R. -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 shared libraries.
Objects are initialized as follows:
Sequence | Object | Priority value | Comment |
---|---|---|---|
1 | libS6 | n/a | libS2 was entered last on the command line when linked with main, and so is initialized before libS1. However, libS5 and libS6 are dependencies of libS2, so they are initialized first. Since it was entered last on the command line when linked to create libS2, libS6 is initialized first. The objects in this library are initialized according to their priority. |
2 | fileL | 600 | The objects in fileL are initialized next (lowest priority number in this module). |
3 | fileK | 65535 | The objects in fileK are initialized next (next priority number in this module (default priority of 65535)). |
4 | libS5 | n/a | libS5 was entered before libS6 on the command line when linked with libS2, so it is initialized next. The objects in this library are initialized according to their priority. |
5 | fileI | 500 | The objects in fileI are initialized next (lowest priority number in this module). |
6 | fileJ | 65535 | The objects in fileJ are initialized next (next priority number in this module (default priority of 65535)). |
7 | libS4 | n/a | libS4 is a dependency of libS1 and was entered last on the command line when linked to create libS1, so it is initialized next. The objects in this library are initialized according to their priority. |
8 | fileH | 200 | The objects in fileH are initialized next (lowest priority number in this module). |
9 | fileG | 300 | The objects in fileG are initialized next (next priority number in this module). |
10 | libS3 | n/a | libS3 is a dependency of libS1 and was entered first on the command line during the linking with libS1, so it is initialized next. The objects in this library are initialized according to their priority. |
11 | fileF | 65535 | Both fileF and fileE are assigned a default priority of 65535. However, because fileF was listed last on the command line when the object files were linked into libS3, fileF is initialized first. |
12 | fileE | 65535 | Initialized next. |
13 | libS2 | n/a | libS2 is initialized next. The objects in this library are initialized according to their priority. |
14 | fileD | 65535 | Both fileD and fileC are assigned a default priority of 65535. However, because fileD was listed last on the command line when the object files were linked into libS2, fileD is initialized first. |
15 | fileC | 65535 | Initialized next. |
16 | libS1 | libS1 is initialized next. The objects in this library are initialized according to their priority. | |
17 | fileA | 101 | The objects in fileA are initialized next (lowest priority number in this module). |
18 | fileB | 150 | The objects in fileB are initialized next (next priority number in this module). |
19 | main.out | n/a | Initialized last. The objects in main.out are initialized according to their priority. |