-1

I'm not very used and don't really understand the use of global variables in c++, and cannot manage to solve a problem here, although I'm nearly sure I'm using the declarations the right way. I've got an unordered map, which contains params from a file. This is working fine, I've outputted the contents, everything ok. But I would like this map to be available everywhere in my program, so that I don't have to pass it as an argument every time, hence the use of global variable. I've a little typedef :

typedef tr1::unordered_map<string,float> unmap;

then the function to read the parameters:

unmap readParameters(string filename);

then the global scope declaration (everything outside the main of course, although in the same file, I still don't have any headers):

extern unmap params;

The affectation is made in the main() function:

unmap params=readParameters(inputfile_name);

(Note: I tried with and without the "unmap", but same error). And I've got a function displayInformation() to display its contents in which I use my map "params":

for (unmap::iterator it=params.begin();it!=params.end();it++){
    cout<<it->first<<" = "<<it->second<<endl;
}

But g++ moans then :

    Undefined symbols for architecture x86_64:
  "_params", referenced from:
      _main in loca.o
      displayInformation()     in loca.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [loca] Error 1

So how should I declare/use this to be able to see it from any function?

François Laenen
  • 171
  • 4
  • 14
  • http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c i think that is explained there in detail. – fonZ May 11 '13 at 15:37
  • Thank you I figured it out! I already read this post but it seems that i misplaced the declaration of the variable. – François Laenen May 14 '13 at 09:29

2 Answers2

3

The extern declaration tells the compiler that you have defined the variable elsewhere. The error you are seeing is down to the fact that you have not defined the variable anywhere. You need to add a line like the following:

unmap params;

However, in your case, you can just remove the extern declaration as it is unnecessary if the global variable is only used in the same file it is defined. Typically you put an extern declaration in a header file when you define the global variable in one .cpp file, and then use it in another. extern is essentially saying "there is a variable somewhere named X of type Y", but it does not actually define that variable.

user2093113
  • 3,230
  • 1
  • 14
  • 21
  • "not defined the variable anywhere". I feel so dumb, I changed the name of my constants in the source file, which was why the header wasn't matching the source file. – Michael Ozeryansky Jun 19 '15 at 18:42
3

the best practice is to make a header file and call it global.h or something else. Everywhere you need your global variable you include the header file. In the header file you add the following code:

//global.h
#include<map>
extern std::unordered_map<string, float> unmap;

the next step is to define the global variable that can you do for example in the main.cpp

//main.cpp
#include"global.h"

//definition of the global variable (just once)
std::unordered_map<string, float> unmap;

int main() {
unmap = readParameters("fileName");
}

now you can use the global map everywhere you include the file global.h

I hope that was what you have looked for

tzwickl
  • 1,341
  • 2
  • 15
  • 31