Connecting Tech Pros Worldwide Help | Site Map

Linkage errors

zahy[dot]bnaya[At]gmail[dot]com
Guest
 
Posts: n/a
#1: May 27 '06
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.
[color=blue]
>From my main file (where the main() is) I include only the cpp files[/color]

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.

Ian Collins
Guest
 
Posts: n/a
#2: May 27 '06

re: Linkage errors


zahy[dot]bnaya[At]gmail[dot]com wrote:[color=blue]
> Hello,
>
> I am using MSVC 6.[/color]
Never mind...
[color=blue]
> I organize my files so that Class definitions are in a .H files and the
> implementation is on a .cpp files.
>[color=green]
>>From my main file (where the main() is) I include only the cpp files[/color]
>
> so my main file looks like that:
>
> #include "foo.cpp"
> #include "bar.cpp"
>[/color]
NO! Don't do this.

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

Should be int main.


--
Ian Collins.
zahy[dot]bnaya[At]gmail[dot]com
Guest
 
Posts: n/a
#3: May 27 '06

re: Linkage errors


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?

Ian Collins
Guest
 
Posts: n/a
#4: May 27 '06

re: Linkage errors


zahy[dot]bnaya[At]gmail[dot]com wrote:[color=blue]
> Thanks for your response Ian,
>[/color]
Please read <http://cfaj.freeshell.org/google/> for good advice on
posting from google.
[color=blue]
> So my main file should be clean of any includes to my own files?[/color]

Includes of your source files, yes.
[color=blue]
> How would the linker know about them then?[/color]

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.
[color=blue]
> should I include only the header files?
>[/color]
Yes. That way the compiler gets to know the classes and functions you
define in your other source files.

--
Ian Collins.
Closed Thread