Skip to main content
GameDev.net gamedev.net
🔒 Locked

Linker Error Help

Started by sdrichardson Mar 30, 2004 at 12:42 PM 4 replies 200+ views
Original Post
sdrichardson
sdrichardson
i''m getting a lot of linker errors stating a particular function is "already defined in something.obj... for example: Chess3D v0.1 error LNK2005: "public: float __thiscall TDirect3dApp::GetBoardCoOrds(int)" (?GetBoardCoOrds@TDirect3dApp@@QAEMH@Z) already defined in Direct3d.obj could anyone tell me what causes this or how to fix it? Scotty.
Scotty.
ToohrVyk
ToohrVyk
This happens when an object (in your case, a function) is defined in two different cpp files.

Also note that if an object is defined in a file, it will also be defined in all files that include that file. No, include guards (the #ifndef #define #endif tags) don't protect you against this.

Be careful to
- never define things in header files
- never include cpp files in anything
and this kind of problem should not happen.

EDIT: and by "defined" I mean writing things like :
int Variable; //Variable is defined
void Function( void ) { return; } //Function is defined

What you CAN do, however, is things like :
extern int Variable; // Variable is declared
void Function( void ); //Function is declared

What you usually do is define a thing in only ONE place, then declare it everywhere where you need it.

Victor Nicollet, INT13 game programmer



[edited by - ToohrVyk on March 30, 2004 1:50:09 PM]
Fruny
Fruny
quote:
Original post by ToohrVyk
- never declare things in header files



Declarations are fine. Definitions aren't.

Declarations:

extern int foo;
int bar();
class baz;

Definitions:

int foo;
int bar() {}
class baz {};


[edited by - Fruny on March 30, 2004 1:48:27 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
sdrichardson
sdrichardson
all is well now, thank you!

Scotty.
Scotty.
ToohrVyk
ToohrVyk
Argh, Fruny, if only you knew...
Every time I wanted to type "define" I typed "declare", and had to write it again. I guess I missed one... But I edited it before you answered

(BTW: concours (Mines, puis Centrale, puis X, puis ENS) dans 27 jours... stress)

Victor Nicollet, INT13 game programmer



[edited by - ToohrVyk on March 30, 2004 1:57:23 PM]
Fruny
Fruny
quote:
Original post by ToohrVyk
(BTW: concours dans 27 jours... stress)



Quelle école?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.