#### Archived

This topic is now archived and is closed to further replies.

# LNK2005!!!! It's driving me mad!!

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

## Recommended Posts

VC++ keeps giving me the error LNK2005, saying that my functions that I have in a header file have already been defined in my main game manger class .obj file. Does anyone know what could cause this!! The functions are obviously not declared in the Game class files, so what could be leading VC++ to think that they are? Could someone please help?

##### Share on other sites
Are you using inclusion guards?

Do any .h files include .cpp or .h files?

Do any .cpp files include other .cpp files?

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
are you missing a brace? that seems to set it off also too.

##### Share on other sites
Usually that means that you have a function inside of a header file that has also been defined in another source file that redefines that same function. Make sense?

Michael Bartman
Dark Omen Studios

[edited by - TheBartman on November 8, 2002 2:10:34 AM]

##### Share on other sites
what''s the exact message?

##### Share on other sites
Fancy that, i''ve come looking for a solution to the exact same problem!

the message is

StdAfx.obj : error LNK2005: "class CCamera * g_Camera" (?g_Camera@@3PAVCCamera@@A) already defined in Game.obj
WPMachineGun.obj : error LNK2005: "class CCamera * g_Camera" (?g_Camera@@3PAVCCamera@@A) already defined in Game.obj

##### Share on other sites
johndunne: Your problem is that you must define g_camera as an extern in whatever .h file has it. Then you declare g_camera again ONLY in the matching .cpp file.

So if you had it in game.h, you would do:

--- Game.h ---

extern CCamera* g_camera;

--- Game.cpp ---

CCamera* g_camera = NULL;

##### Share on other sites
More then likely you have defined something twice, such as two classes of the same name etc

I find if I have my main.h and main.cpp
I only need to inlcude the main.h in the main.cpp
this holds all my functions declarations, variables etc

If I include main.h anywhere else even with #ifndef''s in main I get the above error. I only include it in main.cpp along with all my other .h classes and their .cpp files....make sense?

But also look out for old functions you forgot to delete etc....
you may have declared a new one, whats the exact error???

##### Share on other sites
Consider the following "main.h" file:

int myVariable;

If i include that in my main.cpp file, the row that says
#include "main.h"
will be replaced by:
int myVariable;

The same thing will happen in any other file you include main.h in. Result? You get several copies of the definition of the myVariable integer. Solution? Like someone already mentioned, don't define the variables in a .h-file, just declare it:

// main.h:extern int myVariable;void cool(void);// main.cpp#include <iostream>#include "main.h"int myVariable = 0;using std::cout;using std::endl;int main(void) {  cool();  cout << myVariable << endl;  return 0;}// cool.cpp#include "main.h"void cool(void) {  myVariable++;}

... which prints 1.

This whole thing will result in a global variable myVariable that can be seen from any file that includes main.h but is only defined once, thus doesnt give any errors. As seen by the fact that cool uses the same myVariable.

The key to this is the extern keyword in the header file which converts the copied definition to a copied declaration.

[edited by - Stary on November 8, 2002 9:13:16 PM]