Re: Make HWND global LNK2005
Quote:
Originally Posted by
ImNotaBot
hey folks,
im codin a win32 app. and i want to make the HWND hWnd to a global variable. well i put all this gui stuff in a file GUI.h and GUI.cpp. now i want to make the window handle global so i can acces it if i include the header. Now my GUI.h looks like that:
When you use an include file, all you're doing is taking the text from the include file and placing it into your source file. Nothing more than that. So what does GUI.CPP and Main.cpp end up with?
You now have a declaration of MyWindowHandle in two modules. Now when the linker looks at both a.obj and b.obj, it sees two declarations of MyWindowHandle. The linker then gives the error that you have multiple definitions of MyWindowHandle.
One solution is to use the extern keyword for the globals in one module, and not use extern in the other module -- this has been discussed here many times (so a search will bring up those threads). Also any good C++ book explains the usage of extern for global variables.
But that is an inferior solution -- the real "solution" is to not use global variables and design your code differently so that there is no need for globals.
Regards,
Paul McKenzie
Re: Make HWND global LNK2005
To complete a little what Paul already stated:
Generally, in a Win32 application it's not necessary to store window handles.
That's because the window procedures get the window handle as parameter, then you can further pass it to handler functions.
Also, the notification messages come with the child window handles.
Also, there are WinAPI functions which can easily get a window handle (e.g. GetDlgItem).
And so on...
[ Moved thread ]
Re: Make HWND global LNK2005
Okay, but in fact I cant see *where* i declared MyWindowhandle two times, thats what surprised me. Do you see it? I just declared it in the GUI.h Nowhere else. I think my linker is kidding me :)
Re: Make HWND global LNK2005
Quote:
Originally Posted by
ImNotaBot
Okay, but in fact I cant see *where* i declared MyWindowhandle two times, thats what surprised me. Do you see it?
I see it clearly.
Quote:
I just declared it in the GUI.h Nowhere else. I think my linker is kidding me :)
Did you read my explanation? I'll quote again:
Quote:
When you use an include file, all you're doing is taking the text from the include file and placing it into your source file.
You did this:
In both your source files. So what does GUI.h contain?
Regards,
Paul McKenzie
Re: Make HWND global LNK2005
Quote:
I just declared it in the GUI.h Nowhere else.
You have to learn what #include actually does.
Re: Make HWND global LNK2005
Quote:
Originally Posted by
ImNotaBot
I think my linker is kidding me :)
A linker is never kidding. It just reports the mistakes you made.
Re: Make HWND global LNK2005
Okay now i understood what u want to tell me. But is this
Code:
#ifndef somethingThatShouldNotBeDuplicated
#define somethingThatShouldNotBeDuplicated
#endif
not exactly to avoid the duplictated inclusion?
greets and thanks for help.
Re: Make HWND global LNK2005
Quote:
Originally Posted by
ImNotaBot
Okay now i understood what u want to tell me. But is this
Code:
#ifndef somethingThatShouldNotBeDuplicated
#define somethingThatShouldNotBeDuplicated
#endif
not exactly to avoid the duplictated inclusion?
Indeed it is "to avoid the duplictated inclusion" in the SAME module. But it cannot avoid the inclusion in another module!
Re: Make HWND global LNK2005
Quote:
Originally Posted by
ImNotaBot
Okay now i understood what u want to tell me. But is this
Code:
#ifndef somethingThatShouldNotBeDuplicated
#define somethingThatShouldNotBeDuplicated
#endif
not exactly to avoid the duplictated inclusion?
I'll quote once again:
Quote:
When you use an include file, all you're doing is taking the text from the include file and placing it into your source file.
You're still not taking that quote and understanding it clearly.
You have two different modules, A.CPP and B.CPP. Each one of those files has #included that GUI.H header. So again, take the text in that header and copy/paste it in each one of those modules. That is what you end up with, and that is what the compiler is compiling.
Now ask yourself, how is A.CPP and B.CPP supposed to know that each other has seen your definition? What is this magic communication that each has? There isn't any. All the compiler sees is what it sees.
Now you have two object files created from compiling, A.OBJ and B.OBJ, so they still have that declaration in each one, and the linker sees both definitions.
Regards,
Paul McKenzie