| | |
Error linking C object file to C++
Thread Solved |
Hey,
I have a program that has 5 files, 2 header files called:
and 3 source files called:
When I copy the
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:
in both my a2main.cpp and screen.cpp as the objects and functions in them are called by the main.
Thanks.
I have a program that has 5 files, 2 header files called:
console.h screen.h
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:
C++ Syntax (Toggle Plain Text)
extern "C" { #include "console.h" }
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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
•
•
•
•
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.
.
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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
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
Now, in one and only one *.cpp file you have to declare them without the extern keyword
Here is an example of how you must declare variables in header files
C++ Syntax (Toggle Plain Text)
// consol.h #ifdef _cplusplus extern "C" { #endif extern int _line; extern int _cursor_pos; extern int _col; #ifdef _cplusplus }; #endif
Now, in one and only one *.cpp file you have to declare them without the extern keyword
C++ Syntax (Toggle Plain Text)
//console.c #ifdef _cplusplus extern "C" { #endif int _line = 0; int _cursor_pos = 0; int _col = 0; #ifdef _cplusplus }; #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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
>>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.
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
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
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
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
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
![]() |
Similar Threads
- need help on linking error in C code!! (C)
- Linking classes within header files into main (C++)
- trouble seperating classes outside of main file (C++)
- Turbo C Fixup Error (Legacy and Other Languages)
- file linking problem (C++)
- Makefile for C++ one file programs (C++)
- error: no match for ‘operator<<’ in ‘std::cout (C++)
- Connecting To External Javascript File (JavaScript / DHTML / AJAX)
- C++ linking problem (C++)
- IEDLL.EXE and Loader.exe error (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Problem with Structs in arrays, and files
- Next Thread: Search engine
Views: 825 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee image input int lazy link linked-list linker list loop loops math matrix member memory newbie number object objects opengl output parameter path pointer pointers problem program programming project python random read reading recursion recursive reference return search server sort spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock wxwidgets






