c++aixstatic-initializationxlcstatic-order-fiasco

Does IBM XL C/C++ provide an init_priority?


We use C++ static initialization order to control the construction of several objects in a C++ library. GCC offers init_priority, and Microsoft offers init_seg().

I'm looking for similar with IBM's XL C/C++ compiler, but my search results are not revealing useful hits. The compiler runs on both Linux and AIX. Obviously Linux offers the feature, but I am not sure about AIX runtime support.

My first question is, does the AIX platform support initialization priorities?

My second question is, does IBM's XL C/C++ offer a way to control it via the source code?


Solution

  • IBM XL C/C++ for Linux V13.1.1 and up does offer a way to control initialization priorities, please see the Knowledge Center for more details about how to control it via the init_priority variable attribute, similar to GCC, which assigns static initialization priorities at the object level.

    IBM XL C/C++ for AIX does not support init_priority but it does offer a way to control the order of initialization of global variables between .o files within the same link unit with -qpriority and #pragma priority. Details about those options can be found on Knowledge Center. In addition, -qmkshrobj=priority (Knowledge Center) controls the order of initialization of shared objects (and a.out) at program start up (XL AIX uses this method, whereas XL Linux uses dependency to determine shared object initialization order). The order within a translation unit has to be in declarative order otherwise you can get initialization referencing a variable that hasn't been initialized yet.