Error linking C object file to C++

Thread Solved

Join Date: May 2008
Posts: 131
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 10
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Error linking C object file to C++

 
0
  #1
May 7th, 2009
Hey,

I have a program that has 5 files, 2 header files called:
console.h
screen.h
and 3 source files called:
a2main.cpp
screen.cpp
console.c    //not a typo

When I copy the main from my a2main.cpp file to my screen.cpp my program compiles without a problem. But when I try and compile it using the external main I get the following errors.
screen.obj : error LNK2005: _line already defined in a2main.obj
screen.obj : error LNK2005: _cursor_pos already defined in a2main.obj
screen.obj : error LNK2005: _col already defined in a2main.obj
a2main.exe : fatal error LNK1169: one or more multiply defined symbols found

Could someone please explain why I am getting these errors.
Also please note that the variables mentioned in the errors are not used at all in a2main.cpp they were declared in console.c then redeclared in screen.cpp.

I have used:
  1. extern "C" {
  2. #include "console.h"
  3. }
in both my a2main.cpp and screen.cpp as the objects and functions in them are called by the main.

Thanks.
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 131
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 10
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: Error linking C object file to C++

 
0
  #2
May 7th, 2009
Originally Posted by kenji View Post
Also please note that the variables mentioned in the errors are not used at all in a2main.cpp they were declared in console.c then redeclared in screen.cpp.
.
The above should be:
Also please note that the variables mentioned in the errors are not used at all in a2main.cpp they were defined in console.c then redefined in screen.cpp, but declared in console.h
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,631
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: 1615
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Error linking C object file to C++

 
0
  #3
May 7th, 2009
The most common reason for those duplicate declaration errors is declaring variables in header files without the extern keyword. extern tells the compiler that the object is actually declared in some other *.cpp or *.c file.

Here is an example of how you must declare variables in header files
  1. // consol.h
  2. #ifdef _cplusplus
  3. extern "C" {
  4. #endif
  5. extern int _line;
  6. extern int _cursor_pos;
  7. extern int _col;
  8. #ifdef _cplusplus
  9. };
  10. #endif

Now, in one and only one *.cpp file you have to declare them without the extern keyword
  1. //console.c
  2. #ifdef _cplusplus
  3. extern "C" {
  4. #endif
  5. int _line = 0;
  6. int _cursor_pos = 0;
  7. int _col = 0;
  8. #ifdef _cplusplus
  9. };
  10. #endif
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,631
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: 1615
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Error linking C object file to C++

 
0
  #4
May 7th, 2009
>>Also please note that the variables mentioned in the errors are not used at all in a2main.cpp they were defined in console.c then redefined in screen.cpp,

You can't declare the same variables in two or more *.cpp files. Declare them in only one *.cpp file and use the extern keyword to declare them on other files. See example in my previous post.

Some compilers will discard unused variables during the optimization process, but apparently yours does not. In the case of your program I doubt any compiler would discard them because it doesn't know until link time whether the variables are actually used or not.
Last edited by Ancient Dragon; May 7th, 2009 at 9:47 pm.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 131
Reputation: kenji is an unknown quantity at this point 
Solved Threads: 10
kenji's Avatar
kenji kenji is offline Offline
Junior Poster

Re: Error linking C object file to C++

 
0
  #5
May 7th, 2009
Why is it then that I'm able to compile the program when my main is a part of the screen.cpp file instead of the main.cpp?
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,631
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: 1615
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Error linking C object file to C++

 
0
  #6
May 7th, 2009
you would have to post all that code -- it shouldn't matter what *.cpp file contains main().
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 825 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC