Linkage errors

  • Thread starter zahy[dot]bnaya[At]gmail[dot]com
  • Start date
Z

zahy[dot]bnaya[At]gmail[dot]com

Hello,

I am using MSVC 6.
I organize my files so that Class definitions are in a .H files and the
implementation is on a .cpp files.
From my main file (where the main() is) I include only the cpp files

so my main file looks like that:

#include "foo.cpp"
#include "bar.cpp"

void main(int argc,char* argv[])
{

}


Every cpp file is including its H file.
for example the foo.cpp file would have

#include "foo.h"

on its first lines.

Every .h file is wrapped with a preprocessor indicators :

#ifndef __FOO__H__
#define __FOO__H__

class foo{
....
};

#endif /*__FOO__H__*/


I noticed I tend to get enormous amount of linkage errors like below:

error LNK2005: "public: __thiscall Component::Component(void)"
(??0Component@@QAE@XZ) already defined in linkedlist.obj
tmp.obj : error LNK2005: "public: __thiscall Component::Component(int)"
(??0Component@@QAE@H@Z) already defined in linkedlist.obj
tmp.obj : error LNK2005: "public: __thiscall
Component::~Component(void)" (??1Component@@QAE@XZ) already defined in
linkedlist.obj


why is this happening? I also noticed that it does not occur on all of
my project but only once in a while.. why is that?
There must be a simple answer to that...
Am I doing something wrong? is this a good organization of code?

Thanks.
 
I

Ian Collins

zahy[dot]bnaya[At]gmail[dot]com said:
Hello,

I am using MSVC 6.
Never mind...
I organize my files so that Class definitions are in a .H files and the
implementation is on a .cpp files.


so my main file looks like that:

#include "foo.cpp"
#include "bar.cpp"
NO! Don't do this.

Compile your source files and link them with your main file.
void main(int argc,char* argv[])

Should be int main.
 
Z

zahy[dot]bnaya[At]gmail[dot]com

Thanks for your response Ian,

So my main file should be clean of any includes to my own files?
How would the linker know about them then? should I include only the
header files?
 
I

Ian Collins

zahy[dot]bnaya[At]gmail[dot]com said:
Thanks for your response Ian,
So my main file should be clean of any includes to my own files?

Includes of your source files, yes.
How would the linker know about them then?

That will be described in you documentation, if you are using an IDE, it
should support building projects where you can include all of your
source files.
should I include only the header files?
Yes. That way the compiler gets to know the classes and functions you
define in your other source files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top