Alf P. Steinbach said:
* Milan Radovich:
Translation unit [a.cc] is referenced & therefore included by
virtue of containing "main", but there's no reference to anything
in [b.cc].
So, a simple fix is to reference something in [b.cc], e.g. a call
of some dummy function there.
According to Pete's interpretation (see his reply to my post in the same
discussion subthread you are referring to) this is not enough: you
may need to reference a variable who's initialization should have a
side effect. Referencing some dummy function won't be enough unless
that function uses the said variable.
In my case: if you add dummy function "void f(){}" to b.cc and call
it from a.cc, initialization of bar is implementation detail and won't
necessarily occur. In order to ensure bar's initialization, bar itself
should be referenced from a.cc (or f should use bar).
This seems overly, extremely pessimistic. I'm sure that it would be
mentioned in the standard if it was intended to be this way. But
despite some extensive debate in this thread, no such mention in the
standard has been found.
Instead the standard goes to great lengths to explicitly point out
what can't be eliminated.
In particular §3.7.1/2 "If an object of static storage duration has
initialization or a destructor with side effects, it shall not be
eliminated even if it appears to be unused, except that a class object
or its copy may be eliminated as specified in 12.8", as I've mentioned
& quoted a few times.
But Alf, I understood from the discussion so far that there is indeed
only a requirement that code that is required is linked in, to satisfy
external references; that, whilst it /is/ the case that g++ will link in
a whole translation unit (object file) from a library, that this is
indeed implementation dependent - that there is no requirement that it
do so. Hence, for portability reason, I would say that Milan's concern
is a valid one.
As Pete referenced in an earlier post:
[lex.phases] 2.2/1-9
"All external entity references are resolved. Library components are
linked to satisfy external references to entities not defined in the
current translation. ..."
It is only the fact that g++ is not applying a "smart linking" model -
bringing in *only* what is required to satisfy external references -
that the kind of initialization that Milan is looking for is not
guaranteed. As has been seen, g++ can guarantee this as an
implementation detail, but the Standard doesn't require it, as long as
external references are satisfied.