__attribute__((constructor)) call order confusion

前端 未结 4 1074
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 02:55

The answer here demonstrates that __attribute__((constructor)) is not called after static initialization, it is called in the declaration order.

Then, w

4条回答
  •  甜味超标
    2021-02-06 03:16

    See this: http://gcc.gnu.org/ml/gcc-help/2011-05/msg00220.html and the answer http://gcc.gnu.org/ml/gcc-help/2011-05/msg00221.html

    In particular quoting from the answer:

    All objects with an init_priority attribute are constructed before any object with no init_priority attribute.

    Note it is actually about __attribute__((init_priority)) and not about __attribute__((constructor)) but I believe they both actually use the same code in gcc, respectively in gnu linker. First just corresponds to C++ objects, i.e. calling their constructor/destructor, the latter is about marking specific functions as constructor or destructor.

    IMHO, the __attribute__((constructor)) exists mainly because of C, not C++.

提交回复
热议问题