Initializing static objects in libraries (C++)
The C++ language definition specifies that all non-local objects
with constructors from all the files included in the program must
be properly constructed before the main function
in a C++ program is executed.
You can assign a priority level number to objects and files within a single library using the following approaches. The objects will be initialized at run time according to the order of priority level. In addition, because modules are loaded and objects are initialized differently on different platforms, you can choose an approach that fits the platform better.
- Set the priority level for an entire file
- To use this approach, specify the -qpriority compiler option during compilation. By default, all objects within a single file are assigned the same priority level; they are initialized in the order in which they are declared, and they are terminated in reverse declaration order.
- Set the priority level for individual objects
- To use this approach, use
init_priorityvariable attributes in the source files. Theinit_priorityattribute can be applied to objects in any declaration order. On Linux®, the objects are initialized according to their priority and terminated in reverse priority across compilation units.
Priority numbers can range from 101 to 65535. The smallest priority number that you can specify, 101, is initialized first. The largest priority number, 65535, is initialized last. If you do not specify a priority level, the default priority is 65535.