Connecting Tech Pros Worldwide Help | Site Map

Linkage errors

  #1  
Old May 27th, 2006, 01:35 AM
zahy[dot]bnaya[At]gmail[dot]com
Guest
 
Posts: n/a
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.

  #2  
Old May 27th, 2006, 01:35 AM
Ian Collins
Guest
 
Posts: n/a

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.
  #3  
Old May 27th, 2006, 01:45 AM
zahy[dot]bnaya[At]gmail[dot]com
Guest
 
Posts: n/a

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?

  #4  
Old May 27th, 2006, 02:25 AM
Ian Collins
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linkage errors after function rename RK answers 0 October 12th, 2007 10:55 PM
linkage errors in release build with VS2005 2b|!2b==? answers 2 March 30th, 2007 03:25 AM
DLL Linkage errors in vc++ mickey22 answers 1 March 1st, 2007 10:04 PM
Creating object from the child of an abstract class causes to linkage errors romayankin@gmail.com answers 6 March 9th, 2006 07:25 PM