Global Variables and Scope - C ++

I'm having a little problem creating a global variable. I am using Visual Studio 2008 and standard C ++.

I have two projects, one is a static library, and the second is a test program that uses this library. I have a global variable in global.h, for example

#ifndef GLOBAL_H #define GLOBAL_H #include <string> extern std::string globalWord; #endif // GLOBAL_H! 

I have global.cpp where I initialize this variable. This variable is used inside my library project. I set the value of this variable from a test project, but this value is not reflected in the library project.

I debugged and shows the new value in the test project, but when the control reaches the library project, this variable value shows empty. So this global range of variables is limited only by the project in which it belongs?

Or is there a better way to do this? I do not want to change the parameters of my function or constructor in my library in order to pass this value.

Any help would be great.

Edit:

This is how this variable is declared in global.cpp

 #include <string> #include "../global.h" std::string globalWord = ""; 

This is how I used it in my library

 #include "../global.h" string text = globalWord; 

thanks

+4
source share
4 answers

Do not use global variables. Just do not. Much better if you have globally accessible data, you should use a global function that returns globalWord, for example:

 std::string globalWord() { static std::string word("Hi Mom"); return word; } 

This will save you from problems with the initialization order (see "Effective C ++ Element # 4").

+7
source

With the keyword "extern", you tell the compiler that the actual variable exists elsewhere. You must also create a variable with the same name without extern, in one and only one place. Usually you get an error message from the linker if you define two of them, but if one is in the library and one cannot help but understand this.

Edit: Make sure the global.cpp file is only in the library or test program, and not both.

+5
source

The problem is likely to be one of the initialization procedures. When a program is connected, there are 2 places where globalWord used during initialization:

  • when initializing text ( "string text = globalWord; ")
  • initialization of globalWord itself

Unfortunately, the C ++ standard does not specify the initialization order of global variables that come from different modules. Something like Matt's answer to using a function or a simple class (such as singleton) to access a global value is the usual way to force a certain initialization order to be executed.

The C ++ FAQ is a little bit about this - if you plan to change globalWord in your program, the situation is a bit more complicated than they are discussing, because they don't seem to consider setting the value hidden behind the "build on first use" function. Usually something like this requires something like a singleton class .

+3
source

The type of behavior you describe is more like a problem when you have a DLL, but you say your library is static, which looks weird.

In any case, take care of global variables in several libraries, if you have a common code library (DLL), you will get a value for each part. Check out this question may be helpful.

+2
source

All Articles