• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Redefined symbols on link

Hello,

I have a VC6 project I'm trying to compile however I get a ton of "already defined" messages on linking. I am pretty sure this is due to including files where they shouldn't be so can someone give me a rundown on where includes should go?

Here are the basics of the includes by file:

driver.cpp
#include "..\state.h"

state.cpp
#include "state.h"
#include "packet.h"

player.cpp
#include "player.h"
#include "deck.h"

deck.h

hopper.h
#include "deck.h"

player.h

state.h

Every function I call in player, hopper or deck results in something like this:
state.obj : error LNK2005: "public: __thiscall ADeck::ADeck(void)" (??0ADeck@@QAE@XZ) already defined in StateTester.obj

Thanks for the help
0
jkelly061597
Asked:
jkelly061597
1 Solution
 
efnCommented:
Hi jkelly,

It's impossible to tell exactly what the problem is from the information you have provided, but I will give you some general information that may help.

Something like this:

extern int f();

is a "declaration."  It tells the compiler that the function will exist somewhere, but it doesn't tell the compiler what should happen when the function is called.

Something like this:

int f()
{
return 0;
}

is a "definition."  It tells the compiler what the function should do, so when the compiler sees one of these, it generates some executable code for the function.

In general, you want to arrange things so each function is defined only once in all the object files you link.  (If you define it twice, the linker doesn't know which copy to use to resolve references.)  Normally, you put the definition in a .cpp file and you put a declaration in a .h file.  Then any other file that needs to call the function #includes the .h file.  So code for the function is only generated when you compile the .cpp file.

Your problem could be caused by having definitions in header files.  If you define the ADeck constructor in a header file and include that header file in more than one source file, you will get a multiple reference error as you have seen.  The fix is to define the functions in .cpp files and only declare them, not define them, in header files.

--efn
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now