As a continuation of my previous question in stackoverflow:
Getting LINK error : Extern in C++. How to access the value of a variable which is modified in File A.CPP in another file File B.CPP
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 LNK2001: unresolved external symbol “unsigned int
VarX” (?VarX@@3IA)
My scenario is as follows:
File1.h
extern unsigned int VarX;
File2.cpp
#include File1.h
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.
You have to define
VarXin global scope, which I’m assuming you’re not doing now, since otherwise it wouldn’t even compile: