This is the mail archive of the
mailing list for the GCC project.
Re: Order of initialization of global/static variables
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Yaron Dayagi <yarondayagi at gmail dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Tue, 29 Apr 2014 17:19:58 +0100
- Subject: Re: Order of initialization of global/static variables
- Authentication-results: sourceware.org; auth=none
- References: <CAGgEBVaMHaDNZ=z=EMwkEiz-XC8+VNv738MX1HGEN+fjoTB9eg at mail dot gmail dot com>
On 29 April 2014 16:25, Yaron Dayagi wrote:
> In gcc 4.4.6 we had no problem with the order of initialization.
> In gcc 4.7.2 we do have a problem.
> A CPP file defined GlobalObj_1 (declared extern in the H file):
> CMyClass GlobalObj_1.
> Another CPP file declared GlobalObj_2 (also declared extern in the H
> file).= The CPP file used copy constructor:
> CMyClass GlobalObj_2(GlobalObj_1).
> In 4.4.6 GlobalObj_1 was initialized first. Now GlobalObj_2 is
> initialized = first.
> Is this intentional?
> How do I revert to old behavior?
> The problem is that we have many files with many global variables (const).
This question is inappropriate on this mailing list, which is for
discussing development of GCC, not for help using it. Please take any
follow-up to the gcc-help mailing list instead.
The order of initialization between separate files is not defined, so
your program has a bug and only worked by chance with earlier
versions. See the documentation for the init_priority attribute at
http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html for a
non-portable way to control the order.
(Please send any replies to the gcc-help list not this one, thanks).