Click here to Skip to main content
14,697,682 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Visual Studio errYou should declare it or LNK2005: variable XXX redefined in xxx.obj Pin
Richard MacCutchan26-Nov-15 2:11
mveRichard MacCutchan26-Nov-15 2:11 
GeneralRe: Visual Studio errYou should declare it or LNK2005: variable XXX redefined in xxx.obj Pin
Javier Luis Lopez26-Nov-15 4:29
MemberJavier Luis Lopez26-Nov-15 4:29 
GeneralRe: Visual Studio errYou should declare it or LNK2005: variable XXX redefined in xxx.obj Pin
Richard MacCutchan26-Nov-15 5:26
mveRichard MacCutchan26-Nov-15 5:26 
AnswerRe: Visual Studio error LNK2005: variable XXX redefined in xxx.obj Pin
Daniel Pfeffer26-Nov-15 7:34
professionalDaniel Pfeffer26-Nov-15 7:34 
GeneralRe: Visual Studio error LNK2005: variable XXX redefined in xxx.obj Pin
CPallini26-Nov-15 22:09
mveCPallini26-Nov-15 22:09 
GeneralRe: Visual Studio error LNK2005: variable XXX redefined in xxx.obj Pin
Richard MacCutchan26-Nov-15 23:23
mveRichard MacCutchan26-Nov-15 23:23 
GeneralRe: Visual Studio error LNK2005: variable XXX redefined in xxx.obj Pin
Daniel Pfeffer28-Nov-15 7:51
professionalDaniel Pfeffer28-Nov-15 7:51 
AnswerRe: Visual Studio error LNK2005: variable XXX redefined in xxx.obj Pin
«_Superman_»29-Nov-15 19:46
professional«_Superman_»29-Nov-15 19:46 
The basis for the error is that building an EXE constitutes compiling and linking.
Compilation - Every CPP file, also called a translation unit or compilation unit, is compiled separately to form OBJ files.
Linking - All the OBJ files are linked together to become the EXE.

In your case, during compilation, every OBJ file will contain a symbol called int variable.
This means the compilation of all units are successful.

But when the linker takes all the OBJ files to create an EXE, it finds the same symbol in all translation units and so it becomes ambiguous to it as to which symbol to use.

Having said this, there are two things that you may want to do with global variables.

First
You may want to use a global variable that is shared between all translation units.
This is the answer that you've gotten so far.
The global variable declaration must happen only once so that the linker only finds one symbol for the variable and links all other extern references to that symbol.

Second
You may want to use the same name for the global variable in each translation unit.
For this to happen, declare the global variable as static - static int variable;
This forces the compilation process to not share the symbol among other translation units.

For more information refer to this link - Types of Linkage[^]
«_Superman 
I love work. It gives me something to do between weekends.


Microsoft MVP (Visual C++) (October 2009 - September 2013)

Polymorphism in C

GeneralRe: Visual Studio error LNK2005: variable XXX redefined in xxx.obj Pin
Richard MacCutchan29-Nov-15 23:22
mveRichard MacCutchan29-Nov-15 23:22 
GeneralRe: Visual Studio error LNK2005: variable XXX redefined in xxx.obj Pin
«_Superman_»30-Nov-15 0:07
professional«_Superman_»30-Nov-15 0:07 
GeneralRe: Visual Studio error LNK2005: variable XXX redefined in xxx.obj Pin
Richard MacCutchan30-Nov-15 0:38
mveRichard MacCutchan30-Nov-15 0:38 
GeneralRe: Visual Studio error LNK2005: variable XXX redefined in xxx.obj Pin
«_Superman_»30-Nov-15 0:40
professional«_Superman_»30-Nov-15 0:40 
GeneralRe: Visual Studio error LNK2005: variable XXX redefined in xxx.obj Pin
Richard MacCutchan30-Nov-15 1:01
mveRichard MacCutchan30-Nov-15 1:01 
Questionc langugae Pin
ravi siriki23-Nov-15 2:58
Memberravi siriki23-Nov-15 2:58 
AnswerRe: c langugae Pin
David Crow23-Nov-15 3:38
MemberDavid Crow23-Nov-15 3:38 
QuestionQuestion about malloc a struct Pin
Millenia Nova20-Nov-15 17:02
MemberMillenia Nova20-Nov-15 17:02 
AnswerRe: Question about malloc a struct Pin
Jochen Arndt20-Nov-15 22:45
professionalJochen Arndt20-Nov-15 22:45 
GeneralRe: Question about malloc a struct Pin
Millenia Nova21-Nov-15 22:53
MemberMillenia Nova21-Nov-15 22:53 
GeneralRe: Question about malloc a struct Pin
Jochen Arndt22-Nov-15 1:22
professionalJochen Arndt22-Nov-15 1:22 
PraiseRe: Question about malloc a struct Pin
Millenia Nova22-Nov-15 12:12
MemberMillenia Nova22-Nov-15 12:12 
AnswerRe: Question about malloc a struct Pin
Richard MacCutchan20-Nov-15 22:45
mveRichard MacCutchan20-Nov-15 22:45 
QuestionMFC Printing of multiple views for a single doc Pin
cc.caprani20-Nov-15 3:51
Membercc.caprani20-Nov-15 3:51 
Question. Pin
Brisingr Aerowing19-Nov-15 19:11
professionalBrisingr Aerowing19-Nov-15 19:11 
AnswerRe: Trying to port libcddb to MSVC, having issues... Pin
Jochen Arndt19-Nov-15 22:05
professionalJochen Arndt19-Nov-15 22:05 
QuestionRe: Trying to port libcddb to MSVC, having issues... Pin
CPallini19-Nov-15 22:24
mveCPallini19-Nov-15 22:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.