1

IN my C++ code I want to make use of a variable "VarX" in a file "B" which is actually modified in another file "A". So I had a look @ the following link & used extern concept.

How do I use extern to share variables between source files?

error LNK2005: "unsigned int VarX" (?VarX@@3IA) already defined in ***.obj.

My scenario is as follows:

File1.h
extern unsigned int VarX;

File2.cpp
#include File1.h
unsigned int VarX = 101;

File3.cpp
#include File1.h
unsigned int temp = VarX;

IMP NOTE: In the header file File1.h there are many other structure definitions and also many othe rdefinitions apart from the Extern definition.

Can someone help me in this. How shall I read the Value of VarX which is modified in File2.cpp in another File File3.cpp.

Community
  • 1
  • 1
codeLover
  • 3,720
  • 10
  • 65
  • 121

1 Answers1

1

The problem isn't accessibility, but a multiple definition. The error message is pretty clear, somewhere in the code you're redefining VarX.

Common causes can be:

  • invalid build - have you cleaned the build before compiling?
  • you have multiple unsigned int VarX = 101; or a unsigned int VarX; somewhere (in a header or an implementation file), without an extern declaration.
  • you #include "File2.cpp" somewhere in the code, causing it to be compiled multiple times.

My bet is on the second possiblity.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Yes I removed the multiple declarations. I had declared multiple times after having look @ one of the stackoverflow link "http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c" . Now I removed the multiple declations. But Still I am getting the same LINK error. :( – codeLover Apr 24 '12 at 13:00
  • Yes I did clean the entire solution & rebuilt entire soln. But stil getting same link error. – codeLover Apr 24 '12 at 13:04
  • ANy inputes or pointers in this direction of fixing this prob would be very much appreciated :) Thank you in advance. – codeLover Apr 24 '12 at 13:05
  • This ***.obj is the name of the CPP source files which #include this header file File1.h where I have declared my extern varaible. – codeLover Apr 24 '12 at 13:09
  • In *** .obj "***" is the placeholder for all those CPP source files which #include the header file File1.h which has the extern definition. – codeLover Apr 24 '12 at 13:10
  • @codeLover ok so you get this error for **ALL** files that include File1.h? That means there's something wrong in the header. Post the full code in the header. – Luchian Grigore Apr 24 '12 at 13:11
  • No there is no error in the header file File1.h because, if I remove this extern declaration from the hedaer file than the solution builds perfectly fine. The Link error gets introduced only if I place the extern declaration line "extern unsigned int VarX; – codeLover Apr 24 '12 at 13:22
  • @codeLover I beg to differ. Can't help you if you don't post the code. As it is no, there's no error. – Luchian Grigore Apr 24 '12 at 13:24
  • I studied my code in detail. Your answers helped me and I got rid of the previous error. But now getting a different error. Again a Link error:: error LNK2001: unresolved external symbol unsigned int VarX" (?VarX@@3IA) in file File2.cpp – codeLover Apr 24 '12 at 13:35
  • @codeLover as that's a different issue, please post a different question. – Luchian Grigore Apr 24 '12 at 13:36
  • Pardon me I am not supposed to post d code as a protocol followed @ my workplace. It would be helpful if u can kindly help me figure out the cause and help in fixing this error. Thanks in advance. – codeLover Apr 24 '12 at 13:37
  • No its not a different issue; although the error is different, the issue is not deviating in any way from the title or purpose of my question posted in stackoverflow. – codeLover Apr 24 '12 at 13:39
  • SInce its not a different issue it would be very helpful if you can help me figure out the fix. – codeLover Apr 24 '12 at 13:40
  • @codeLover The code is different now, that's why I'm saying you should post a new question. I'd be glad to help (if I can). – Luchian Grigore Apr 24 '12 at 13:41
  • Ok sure I will post a different question right now & expect a speedy help as always in stackoverflow. Thanks. – codeLover Apr 24 '12 at 13:42