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

error LNK2005...

Started by Ilankt Feb 16, 2005 at 11:17 PM 7 replies 1.3k views
Original Post
Ilankt
Ilankt
I'm trying to use a global function in a class, and I get this errors:

error LNK2005: "void __cdecl ... already defined in main.obj
fatal error LNK1169: one or more multiply defined symbols found


how to avoid this errors?
How appropriate, you fight like a cow!
OrenGL
OrenGL
give a little more information.
Don't shoot! I'm with the science team.....
jflanglois
jflanglois
What do you mean a global function in a class? Do you mean public and/or static? Do you have inclusion guards in your .h files? Some code would help.
The linker error means you have something defined multiple times.

[edit] it says __cdecl, so do you have the implementation of a function in an h file?


Regards,
jflanglois
MaulingMonkey
MaulingMonkey
Instead of:

header.hhvoid pie ( void ){}somesource1.cc#include "header.hh"somesource2.cc#include "header.hh"


Do:

header.hhvoid pie ( void );header.ccvoid pie ( void ){}somesource1.cc#include "header.hh"somesource2.cc#include "header.hh"
Ilankt
Ilankt
MaulingMonkey, thanks, that worked!
thank you :-)
How appropriate, you fight like a cow!
Qw3r7yU10p!
Qw3r7yU10p!
Quote:
Original post by Ilankt
MaulingMonkey, thanks, that worked!
thank you :-)


Do you understand why it worked?
Ilankt
Ilankt
I think so, it's like having 2 functions that take the same parameters, with the same name, so the linking program cant decide which of both of them to use.
am I right?
How appropriate, you fight like a cow!
ToohrVyk
ToohrVyk
Exactly. Including the file header.hh makes your function definition appear in two .cc files which results in two translation units (files generated from your .cc files) containing definitions for that function. The linker then looks for a definition in all translation units, finds two and dies because it doesn't know which to use.

Using a declaration instead of a definition, using « foo( ); », tells the linker what the function looks like, without actually including its definition in the translation unit.
Emmanuel Deloget
Emmanuel Deloget
Quote:
Original post by Ilankt
I think so, it's like having 2 functions that take the same parameters, with the same name, so the linking program cant decide which of both of them to use.
am I right?


Yep. When the C++ file is preprocessed, the whole include file is copied into the C++ file to create a preprocessed file. The file is then compiled and all the symbols which are defined as global in this compilation unit (and this definition is by default - you'll have to change the declaration of your symbol to make them local to the compilation unit) are exported. When you compile 2 C++ file which exports the same symbols, the linker don't know which one he might choose - the linker is not able to know that the code is the same. Therefore it screams.

header files should be used to declare (prototype) symbols, while C++ files should be used to define (implement) them.

Of course, when you'll understand this better, you'll be able to play with it - because, as you may know, there are some syntax manipulation which enable you to define symbols in the header file.

HTH,

Topic Locked

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

Sign in to reply to this topic.