Multiple declaration error during linking

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 19
Reputation: abhishek2301 is an unknown quantity at this point 
Solved Threads: 0
abhishek2301 abhishek2301 is offline Offline
Newbie Poster

Multiple declaration error during linking

 
0
  #1
May 5th, 2009
Hello,

I am stuck with a multiple declaration error during linking.
I have 4 C++ source and corresponding header files(A, B, C,D) and one main source file(Main.cpp).
Apart from that, I have one header file which contains two function declarations and some #define constants (Config.h) and 2 array decl which are used in the functions itself.
I have #included the he Config.h in Main.cpp, A.h, B.h, C.h.
I have also #included A.h in Main.cpp
Actually, I am using one function from Config.h in Main.cpp and another function in A.cpp.
Now, in this situation I have compiled all the source files separately.
When I am trying to create the exec, during linking of A.o, B.o, C.o, D.o and Main.o...I get error of multiple definition of functions and array declarations.

Please suggest to overcome this error!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,500
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Multiple declaration error during linking

 
0
  #2
May 5th, 2009
Put only function prototypes and extern object declarations in header files, such as
  1. // A.h file
  2. extern void foo();
  3. extern int f1;

Now, in one and only one *.cpp file you have to declare the same without the extern keyword.
  1. // A.cpp file
  2. #include "A.h"
  3.  
  4. int f1 = 0;
  5.  
  6. void foo()
  7. {
  8.  
  9. }
Last edited by Ancient Dragon; May 5th, 2009 at 8:52 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 19
Reputation: abhishek2301 is an unknown quantity at this point 
Solved Threads: 0
abhishek2301 abhishek2301 is offline Offline
Newbie Poster

Re: Multiple declaration error during linking

 
0
  #3
May 5th, 2009
Thanks for you reply!!!

But, suppose if I want to keep the array decl and the function implementations in Config.h and I use one function from Config.h in Main.cpp and another function from Config.h in A.cpp.

How to do this way???

Please help!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,500
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Multiple declaration error during linking

 
0
  #4
May 5th, 2009
>>How to do this way???
You don't! Function implementations can not be put in header files for the reason you have already discovered -- duplicate definition errors.
Last edited by Ancient Dragon; May 5th, 2009 at 10:07 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC