This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Clarification of __attribute__ init_priority
- From: Ian Lance Taylor <iant at google dot com>
- To: Vladimir Simonov <sv at sw dot ru>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 17 May 2011 09:44:27 -0700
- Subject: Re: Clarification of __attribute__ init_priority
- References: <4DD2993D.7090606@sw.ru>
Vladimir Simonov <sv@sw.ru> writes:
> 1. "However, GNU C++ allows users to control the order of initialization of objects
> defined at namespace scope with the `init_priority' attribute by
> specifying a relative PRIORITY". Because no "in a given translation
> unit" here does it mean that
> init_priority works over all translation units and has "link unit" scope?
Yes.
> 2. If 1 is true what about anonymous namespace? Is it considered the same namespace
> for different translation units?
I'm not sure what you mean here. init_priority is a global value. All
objects with higher priority are constructed before all objects with
lower priority. This is true whether the objects are defined at global
scope, in a named namespace, or in an anonymous namespace.
> 3. "Note that the particular values of PRIORITY do not matter; only
> their relative ordering." IMO it may be correct only if default
> priority value > 65535.
> But it means that init priority can't be set lover(value greater) then default.
> Is it correct?
All objects with an init_priority attribute are constructed before any
object with no init_priority attribute.
Note that although the documentation doesn't seem to mention it, the
init_priority attribute only works correctly when using the GNU linker
or gold. As far as I know no other linker implements it. So you're
fine on GNU/Linux, but on other operating systems you should confirm
that it works.
Ian