Re: Global and file-static variables in static library
Giovanni Dicanio wrote:
"David Wilkinson" <no-reply@effisols.com> ha scritto nel messaggio
news:e9A6EHUxHHA.4516@TK2MSFTNGP04.phx.gbl...
namespace
{
Base* Create(){return new D1;}
bool auto_register = RegisterPlugin("D1", Create);
}
Initially I thought this might be because I placed the code in an
anonymous namespace ("file-static"), but the same thing happens if I make
auto_register a global variable (using a different name for each plugin).
David: just for test: could you try this:
static bool auto_register = RegisterPlugin....
using the 'static' keyword?
Giov:
It does not seem to make any difference: anonymous namespace,
file-static or global, none of them get initialized when the file is an
a static library and nothing in the file is directly referenced.
It just seems that when .obj files are in a static library, the linker
eliminates stuff more aggressively than when they are stand-alone. This
is reasonable perhaps, because otherwise a static library might
contribute a lot of dead code. But it is a real PITA. What is needed,
perhaps, is some kind of FORCE keyword in the language that requires a
variable or class object to be initialized before main(), which seems to
be the default in all known compilers in the absence of static libraries
(though not, it seems, required by the standard).
--
David Wilkinson
Visual C++ MVP