Connecting Tech Pros Worldwide Help | Site Map

Linkage errors

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 27th, 2006, 12:35 AM
zahy[dot]bnaya[At]gmail[dot]com
Guest
 
Posts: n/a
Default Linkage errors

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, 12:35 AM
Ian Collins
Guest
 
Posts: n/a
Default 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, 12:45 AM
zahy[dot]bnaya[At]gmail[dot]com
Guest
 
Posts: n/a
Default 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, 01:25 AM
Ian Collins
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.