How can I prevent these link errors, besides keeping all variables and functions in main.cpp?
Code:
main.obj : error LNK2005: "struct HINSTANCE__ * hInst" (?hInst@@3PAUHINSTANCE__@@A) already defined in button.obj
main.obj : error LNK2005: "struct HWND__ * hWndCmd1" (?hWndCmd1@@3PAUHWND__@@A) already defined in button.obj
main.obj : error LNK2005: "struct HFONT__ * hFont" (?hFont@@3PAUHFONT__@@A) already defined in button.obj
Release/test.exe : fatal error LNK1169: one or more multiply defined symbols found
The reasons I do not want to keep everything in main.cpp:
1) It is ugly code
2) I have a large project and I like to build incrementally (I think that's right), i.e. the large main.obj does not have to be rebuilt every time small changes are made, only a smaller .obj file.
Is it a good idea to have global variables and function prototypes in a header file, have function definitions in other cpp files, and pass the globals by reference to the functions from the window procedure?
The variables can be declared static so only one instance of them will be created. This seems to work.
Will this cause any problems?
I've never seen a project with all globals declared static.
The problem is that you have to define the global variables in at least one of the .cpp files!! Otherwise, they do not exist! Also, you might wanna use the "extern" keyword to let the compiler know that ur not declaring it in the h file!
I would just copy the variables into the main.cpp and stick extern in front of the header.h ones.