N
Noah Roberts
I'm trying to come up with a way to keep the compiler from optimizing
away objects that it doesn't think are used. I'm using the creation of
static objects to cause behavior to occur before the main() function.
So say I have function `int f();` and inside doit.cpp I have:
namespace {
int x = f();
}
and that's it.
Normally, if doit.cpp is within the program being compiled (at least
with VS2005+) x exists, and since it does f() is called before main,
which is actually what I want to happen.
However, now I'm trying to move doit.cpp into a library and I'm having
no luck at all getting it to happen.
Is there a way to force the compiler into keeping x around?
I don't really need something portable if the only answer is compiler
specific. I'm using MSVC10. I'd prefer something standard though.
away objects that it doesn't think are used. I'm using the creation of
static objects to cause behavior to occur before the main() function.
So say I have function `int f();` and inside doit.cpp I have:
namespace {
int x = f();
}
and that's it.
Normally, if doit.cpp is within the program being compiled (at least
with VS2005+) x exists, and since it does f() is called before main,
which is actually what I want to happen.
However, now I'm trying to move doit.cpp into a library and I'm having
no luck at all getting it to happen.
Is there a way to force the compiler into keeping x around?
I don't really need something portable if the only answer is compiler
specific. I'm using MSVC10. I'd prefer something standard though.