This is the mail archive of the
gcc-help@gcc.gnu.org
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-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Wed, 30 Apr 2014 12:46:34 +0100
- Subject: Re: Order of initialization of global/static variables
- Authentication-results: sourceware.org; auth=none
- References: <CAGgEBVZUTBFOUiBxPvro0Tz5ULZxtAC_LbpLdrcF9QAD0J7Eeg at mail dot gmail dot com> <CAKOQZ8y5W18JwzOLky0C+v9_jtEQmjQ8WTmE5yA-Ae-9ZCmkYg at mail dot gmail dot com>
On 30 April 2014 01:30, Ian Lance Taylor wrote:
> On Tue, Apr 29, 2014 at 8:08 PM, Yaron Dayagi <yarondayagi@gmail.com> 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).
>
> The C++ language does not define the order in which global variables
> in different files are initialized. Any such dependency in your
> program is a potential bug.
>
> That said, yes, GCC 4.7 did change the order. See
> http://gcc.gnu.org/PR46770 . You may be able to change back by
> configuring with --disable-initfini-array. That said, this solution
> is likely to be only temporary. The only permanent fix will be to
> remove the unsupported dependency in your C++ program.
Another option is the init_priority attribute documented at
http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html