1

This is a simple example of what i am trying to achieve: I have a header file GlobalVar.h

#ifndef MY_GLOBALS_H
#define MY_GLOBALS_H

extern int flag;

#endif

A MainWindow.cpp

#include "Globalvar.h"
int flag;
void function() {
qDebug() <<"Flag is"<<flag;
}

A main.cpp

    #include "GlobalVar.h"
    int flag=0;
    int main() {
    if(true) {
    flag=1;
    qDebug() <<"Flag is"<<flag;
    }
}

Now the console output shows the value of flag as : "Flag is 1" (In Line no:61) and "Flag is 0" (In Line no: 80).

This means that the flag has been set to 1 already when it reaches to the MainWindow.cpp then why does to shows the value of flag equal to 0 ?

jmoriarty
  • 17
  • 7
  • 3
    How did this link? You have a duplicate definition of `flag`. – Angew is no longer proud of SO Jun 06 '18 at 12:28
  • 2
    You have two variables named flag. You want to define your extern `int flag;` in one cpp file, not both. – Retired Ninja Jun 06 '18 at 12:28
  • Also there is no call to `function()` - so how you get 2 outputs is a mystery. – Richard Critten Jun 06 '18 at 12:29
  • Earlier I defined flag in main.cpp only but throws error : Error31 error LNK2001: unresolved external symbol "int flag" (?flag@@3HA) – jmoriarty Jun 06 '18 at 12:30
  • All the functions are being called from another function. It's a small part of a big project – jmoriarty Jun 06 '18 at 12:31
  • 3
    Please post a [mcve]. If you post code that either does not compile or can not show you problem you have, how can you expect an answer? – Richard Critten Jun 06 '18 at 12:33
  • What was the complete error message including the obj file the unresolved symbol was in? – Retired Ninja Jun 06 '18 at 12:33
  • I am sorry for some errors in my question : Here is the complete error including the obj file: Error 31 error LNK2001: unresolved external symbol "int flag" (?flag@@3HA) in the MainWindow.obj file – jmoriarty Jun 06 '18 at 12:37
  • aside: Do yourself a favour, and don't do this... global variables are going to lead to pain later; and that later might be sooner than you think – UKMonkey Jun 06 '18 at 13:29
  • @UKMonkey I wish i could do that but it is the requirement of the program to use a global variable – jmoriarty Jun 06 '18 at 13:41
  • If anyone finds the solution! Do Post it, meanwhile I implemented a workaround using singleton class. [link](https://www.geeksforgeeks.org/singleton-class-java/) provides a beautiful insight about singleton classes. I advise anyone who comes here to have a look on it. Thanks! :-) – jmoriarty Jun 07 '18 at 11:01

1 Answers1

2

You declare the global variable flag in MY_GLOBALS_H:

extern int flag;

You then define it in MainWindow.cpp:

int flag; // this essentially means "int flag = 0;"

And then you define it again in main.cpp:

int flag = 0;

You can declare a variable many times but it must be defined exactly once.

Quadir Ali
  • 126
  • 1
  • 4
  • Here's a reference to this question which has been answered previously: https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c – Quadir Ali Jun 06 '18 at 13:20
  • I am sorry but you are wrong. I had to define the flag in MainWindow.cpp because it threw error elsewise (error is given in the comments to the question). I ran a similar program in Qt Creator WITHOUT defining flag in MainWindow.cpp and again I got the same output "flag is 0" from MainWindow and "flag is 1" from main – jmoriarty Jun 06 '18 at 13:40
  • @jmoriarty You have definitions `int flag;` in MainWindow.cpp and `int flag=0;` in main.cpp. i.e _Two_ definitions. One of them doesn't belong. Take one out. – acraig5075 Jun 06 '18 at 13:44
  • I was addressing any linker errors you were experiencing. The only way this program of yours even links is if one of those "int flag;" definitions was made inside a different scope (i.e. within a namespace). Otherwise, you will get a "duplicate symbol" error. This would also explain how the values don't sync up because you essentially have 2 distinct variables. – Quadir Ali Jun 06 '18 at 14:01
  • I get the duplicate symbol error when compiling it with Qt Creator but the output still doesnt matches even after removing the definition and compiling it in Qt Creator where as in VS it throws Link error on removing the definition from MainWindow – jmoriarty Jun 06 '18 at 14:07
  • If anyone finds the solution! Do Post it, meanwhile I implemented a workaround using singleton class. [link](https://www.geeksforgeeks.org/singleton-class-java/) provides a beautiful insight about singleton classes. I advise anyone who comes here to have a look on it. Thanks! :-) – jmoriarty Jun 07 '18 at 11:00