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

Started by
8 comments, last by 987th Yak 21 years, 5 months ago
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?
-----------------------------------------------------------PLEASE don't comment on the pointlessness of this post!!
Advertisement
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.
are you missing a brace? that seems to set it off also too.
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
Lead Engine Programmer


[edited by - TheBartman on November 8, 2002 2:10:34 AM]
Michael BartmanDark Omen StudiosLead Engine Programmer
what''s the exact message?
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

I'm all dunne now
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;
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???
~~~~~~~~~~~~~~~~~~~~~~~~~http://on.to/oni
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]
Thanks for the help guys, my camera now works in all places! Sorry to hijack your thread 987th Yak!
I'm all dunne now

This topic is closed to new replies.

Advertisement