Global Problem (Solved)

This topic is 3901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hiya, I've designed my engine DLL to detect whether a high-resolution timer is available when a process attaches (i.e. in DllMain() ), and if so to set a global counter frequency variable using QueryPerformanceFrequency. The linker reports that the global is already defined, though. The code looks like this:
// Timer.h

namespace engine {

__int64 g_i64CounterFrequency;

}

// DllMain.cpp

#include "Timer.h"

if( !QueryPerformanceFrequency( (LARGE_INTEGER*)g_i64CounterFrequency )
{
// OutputDebugString(...);
}

I'm not sure what's wrong. Any help would be much appreciated, thanks :)

Share on other sites
The global is defined in a header. Thus, it will be defined in every translation unit that ever includes the header.

Don't define variables in headers. If you must partake in this abhorrent practice of global-usage in this fashion, define it in the source file and extern it in the header.

Share on other sites
You usually don't define variables in header files. Rather you should declare them in header files like so:

extern __int64 g_i64CounterFrequency

and then put the definition in a cpp file somewhere
like so:
__int64 g_i64CounterFrequency

Share on other sites
It's fine when I declare it static.

jpetrie - why is using a global for this bad?

Thanks for the replies.

Share on other sites
No declaring it static has not solved your problem. Rather you have now made a separate variable within each cpp file the header is included in. This is almost certianly not the behavior you want.

Like both jpetrie and I have said - declare the variable using extern in the header file and define it in EXACTLY ONE .cpp file.

Share on other sites
Ah, now I see.

Thank you kindly.