C++ constructors

João Cadamuro Junior cadamuro@lit.cpdtt.cefetpr.br
Thu Jun 14 07:53:00 GMT 2001

Jonathan Larmour wrote:

> João Cadamuro Junior wrote:
> >
> > Hello,
> >
> > I have worked with GCC and G++ for "powerpc-eabi" embedded targets. In
> > the last two versions of GCC (2.95.2 and 2.95.3) I have learned some
> > hard things about C++ constructor and GCC.
> >
> > Under gcc-2.95.2 the execution order of constructors is the linkage
> > order. The first file linked will construct their objects first.
> >
> > Under gcc-2.95.3, the behavior is the opposite. The last linked archive
> > will construct their objects first.
> >
> > Since I want to have a c++ code to be compiled correctly independent of
> > the compiler version, someone knows what can I do to force some order to
> > constructors under G++?
> Strictly according to the C++ standard, the execution order of constructors
> is undefined.
> You can control it in G++ though, by compiling with -finit-priority. Then
> you can give your static objects (with constructors) a priority e.g.
> static dummy_init_class dummy_init_obj
>                   __attribute__((init_priority(10000)));
> Jifl
> --
> Red Hat, Rustat House, Clifton Road, Cambridge, UK. Tel: +44 (1223) 271062
> Maybe this world is another planet's Hell -Aldous Huxley || Opinions==mine
> Come to the Red Hat TechWorld open source conference in Brussels!
> Keynotes, techie talks and exhibitions    http://www.redhat-techworld.com/

Thank you Jifl for your g++ tip. It is what I'm looking for, even being a
"non-portable" solution.

Thank you also to Mark Phillips and Bill Randle to share their solutions. Both
can be applied to solve the problem.

But for now I'd like to discuss a little bit more about the "-finit-priority"
g++ flag. When I use this approach the only executed constructors are those who
have the "__atribute__ (init_priority(x))" declared. The other "common" objects
are not constructed. Does anyone know if it is a feature of the GNU C++ when
the flag "-finit-priority" is set, or it is a little "bug"?

Thank you again...

João Cadamuro Junior

Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com

More information about the crossgcc mailing list