Note: | This section applies to C++ programs only. |
In some C++ programs, it is important to specify the order in which objects are initialized. This chapter describes how to use the #pragma priority(N) directive and the -qpriority=N option to specify the initialization order for objects. It also describes how to use the makeC++SharedLib program to construct libraries of shared files containing objects with specified priorities. Finally, it describes how to use the ar command to produce an archive file so that all the objects in the archive file are initialized in the specified order.
Before the main function of a C++ program is executed, the language definition ensures that all objects with constructors from all the files included in the C++ program have been properly constructed. The language definition, however, does not specify the order of initialization for these objects across compilation units. In some cases, you may want to specify the order of initialization of some objects in your program.
Often, your program will be made up of several files and files contained in libraries. The libraries that you use with your C++ source program may contain object (.o) files that have components shared with other programs (shared files), as well as files that are only used by your program (nonshared files).
To specify an initialization order, you can:
Specify an initialization priority number for objects within a file using the #pragma priority(N) directive.
Specify an initialization priority number for a file using the -qpriority=N compiler option, so that the objects within the file have the specified priority.
Specify an initialization priority number for a file using the -qpriority=N compiler option, and specify further initialization priority numbers for objects within the file with the #pragma priority option.
Construct a library of shared files and specify an initialization priority number for the library using the makeC++SharedLib command. Then construct an archive (.a) library containing several shared and nonshared files, each with a priority number specified using the ar command.
The runtime system initializes the objects in these libraries in the order of their priority number. Priority numbers can range from -214783648 to 214783647. However, numbers from -214783648 to -214782624 are reserved for system use. The highest priority you can specify is -214782623, and it is initialized first. The lowest priority, 214783647, is initialized last.
Objects in files with identical priorities are initialized in random order unless you specify -qlanglvl=compat. This option specifies that initialization occurs in link order.
When your program exits, the destructors for global and static objects are invoked in the reverse order of their construction.
If you do not specify priority levels, the default priority is 0 (zero).
The loadAndInit routine will initialize shared libraries built with the makeC++SharedLib command. Likewise, terminateAndUnload will terminate them. Include the file load.h to use these routines, which are found in the libC.a library.
Specifying Priority Levels for Library
Objects
Example of Object Initialization in a
Group of Files
Constructing a Library
ar Command
makeC++SharedLib
Command
langlvl Compiler Option
priority Compiler Option