collect2 and C++ static objects

Andrew Haley aph-gcc@littlepinkcloud.COM
Wed Oct 17 13:17:00 GMT 2007


duncan.loveday@bt.com writes:

 > A question regarding the information at
 > http://gcc.gnu.org/onlinedocs/gcc-4.2.2/gccint/Collect2.html#Collect2,
 > particularly the fact that collect2 will arrange for constructors to be
 > called from the start of the body of main().
 > 
 > What happens if the program relies on constructors being called at an
 > earlier point, for example when initialising one global object from
 > another as below ? The example has a global object "str" being
 > initialised to a copy of another global object "Test::str". On my
 > system, this code runs correctly if the two .C files are built as normal
 > objects and then linked into an executable but crashes if "Test.C" is
 > built as a shared object. With additional debug messages it is a simple
 > matter to show that operators/methods are called on an object that was
 > never constructed.
 > 
 > Or is the example below invalid because it relies on a particular
 > sequence of initialisation ?

It is.  See ISO 14882 Section 3.6.2," Initialization of non-local
objects", in particular the example in Para 3.

Andrew.



More information about the Gcc-help mailing list