Sign in to follow this  

static class in dependent lib MSVC8

Recommended Posts

Hi, I seem to be having trouble with code like this: In a seperate static lib:
//.h file:
class Test {
	Test() {

//.cpp file:
static Test mytestclass;

And in another project (exe); make dependent on the above static lib, and run. I see nothing - any ideas as to why? (Note, it's not just having trouble with stdio, if I try anything else I get the same problems) The same code works fine in VC6. Am I doing something wrong? Thanks!

Share this post

Link to post
Share on other sites
Basically, if you don't use a global variable, the program doesn't need to construct it. In this case it seems like MSVC has decided you never use mytestclass so it doesn't bother.

Share this post

Link to post
Share on other sites
Thanks SiCrane,

Yes, I thought this was happening, but this behaviour seems to be very strangely compiler dependent. It seems VC8,Digital Mars both 'optimize' that code away, VC7 seems to randomly decide to include/exclude some static classes, and VC6 includes them all. I would have thought there would be a compiler option somehwere for it..

As you suggested, if I create another function within the file the static variables are then actually created;

.h file:
extern void hack();
.cpp file:
void hack() {;};

And call hack from the main program - it will cause the static objects to be constructed..

Anyone have any better/ideas solutions than this?


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this