62


What I want to do is just to define a variable in a header file and use it on two different cpp files without redefinition that variable each time I include that header
Here is how I tried :

Variables.h

#ifndef VARIABLES_H // header guards
#define VARIABLES_H

static bool bShouldRegister;

#endif

(I also tried extern but nothing changed)

And in a cpp file I give it a value ::bShouldRegister = true or bShouldRegister = true;

In my another cpp file I check it's value by creating a thread and check its value in a loop (and yes my thread function works well)

 while (true)
 {
     if (::bShouldRegister) // Or if (bShouldRegister)
        {
            MessageBox(NULL,"Value Changed","Done",MB_OK|MB_ICONINFORMATION);
        }
  Sleep(100);
 }

And yes, that MessageBox never appears (bShouldRegister never gets true :/)

Shahriyar
  • 1,483
  • 4
  • 24
  • 37
  • [C++ FAQ 27.15: What's a good coding standard for using global variables?](http://www.parashift.com/c++-faq/global-vars.html) – Dmitry Ledentsov Nov 12 '13 at 14:31
  • @DmitryLedentsov the information you linked is currently dead. Is it the same as [What's a good coding standard for using global variables?, C++ FAQ](http://www.cs.technion.ac.il/users/yechiel/c++-faq/global-vars.html)? – Wolf Sep 29 '16 at 13:46
  • 1
    @Wolf, yes, that used to be the FAQ mirror – Dmitry Ledentsov Sep 29 '16 at 14:29

5 Answers5

91

You should use extern otherwise you will have separated bShouldRegister variables in each translation unit with probably different values.

Put this in a header file (.h):

extern bool bShouldRegister;

Put this in one of implementation files (.cpp):

bool bShouldRegister;

Another way which is simpler is to use inline keyword. Put you variable in a header file as below:

inline bool bShouldRegister;
masoud
  • 55,379
  • 16
  • 141
  • 208
  • As I said I did as you say, but nothing changed – Shahriyar Nov 12 '13 at 12:53
  • So, your code doesn't show how you're using this global value. But the obvious thing is you should use `extern`. – masoud Nov 12 '13 at 12:54
  • I should include my header file in both cpp files, right ? if yes I'm doing this already – Shahriyar Nov 12 '13 at 12:56
  • 1
    You can include the header file everywhere you want. but you should not include the cpp file anywhere. Notice, you should just have one definition for `bShouldRegister` just in a single cpp file. – masoud Nov 12 '13 at 12:59
  • @Shahriyar Did you remove "static bool bShouldRegister;" from the header and change it to "extern bool bShouldRegister;"? – drescherjm Nov 12 '13 at 13:35
32

If you can use C++17, consider using an inline variable:

// in a header file
inline bool bShouldRegister = true;

See How do inline variables work? for more information.

bcd
  • 420
  • 4
  • 5
22

A more C++-like way would be using a class member, syntactically indicated by the static keyword. Class member variables have implicit external linkage.

#ifndef VARIABLES_H
#define VARIABLES_H

class RegUtil {
public:

    static bool bShouldRegister;

};

#endif

in one of your cpp files (maybe variables.cpp), you have to define this class member:

#include "variables.h"

bool RegUtil::bShouldRegister;
Wolf
  • 9,679
  • 7
  • 62
  • 108
  • 9
    imho this is almost more java-like than c++like, i would rather put it inside a namespace and drop the class – 463035818_is_not_an_ai May 22 '18 at 18:38
  • @user463035818 Both options make sense, but I think classes tend to provide narrower contexts than namespaces. I generally try to *avoid* exposing "naked" simple variables, especially bools. PS: yes you're right: I learned about static member variables from Java, but it's also a reasonable option in C++ (I never used Java for production code). – Wolf May 23 '18 at 07:42
11

You need to define the variable in one of the modules:

bool bShouldRegister;

Then declare it extern (not static!) in the header:

extern bool bShouldRegister;
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
-3

Here you need to define bool bShouldRegister in one class. Your header file should like this.

 #ifndef VARIABLES_H // header guards
#define VARIABLES_H

class abc{

public:
      bool bShouldRegister;
      abc();
#endif

Now initialize bShouldRegister variable in cpp file in constructor of abc class and then use this variable in your second cpp file using object of class. You will get your message box.

  • Please have another look on your "answer", it's **syntactically wrong**. – Wolf Feb 27 '18 at 09:05
  • *initialize bShouldRegister variable in cpp file in constructor of abc class* -- this is a **bad advice** for static members. – Wolf Feb 27 '18 at 09:07