PDA

Click to See Complete Forum and Search --> : already defined in *.obj


wey97
Aug 19th, 2002, 05:18 PM
How can I prevent these link errors, besides keeping all variables and functions in main.cpp?

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

VC++6.0 project attached...

wey97
Aug 19th, 2002, 07:53 PM
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?

What is the optimal solution?

abdul
Aug 19th, 2002, 11:51 PM
Try formatting your header files like this:


//File: myheader.h
#ifndef MYHEADER_H
#define MYHEADER_H
//.....
//Header file contents...
//.....
#endif //End of file

wey97
Aug 20th, 2002, 12:00 AM
Thanks, but the header's like this in the project above:

#ifndef __HEADER_H__
#define __HEADER_H__

// header file contents

#endif // __HEADER_H__

BTW: what is the difference in __HEADER_H__ (the way I've seen it) and HEADER_H ?

It doesn't make a difference either way.
Still errors.

wey97
Aug 20th, 2002, 12:13 AM
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. :confused:

MoMad
Aug 20th, 2002, 01:51 AM
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.

Hope that helps,
MoMad the NoMad

wey97
Aug 20th, 2002, 08:33 AM
That seems to work rather well.
I see your point - declare globals in .h and define in .cpp similar to function declarations and definitions.

thanks!

MoMad
Aug 20th, 2002, 02:56 PM
A rather better and cleaner sollution is to put all of ur stuff in classes. It will save you alot of confusion.