Global Problem (Solved)

Started by
4 comments, last by beebs1 16 years, 10 months ago
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 :)
Advertisement
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.
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
It's fine when I declare it static.

jpetrie - why is using a global for this bad?

Thanks for the replies.
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.
Ah, now I see.

Thank you kindly.

This topic is closed to new replies.

Advertisement