Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a global variable in C++

I know one should not use global variables but I have a need for them. I have read that any variable declared outside a function is a global variable. I have done so, but in another *.cpp file, that variable could not be found. So it was not really global. Isn't it so that one has to create a header file GlobalVariabels.h and include that file to any other *cpp file that uses it?

like image 879
Marcus Tik Avatar asked Mar 14 '12 12:03

Marcus Tik


People also ask

How do you declare a global variable?

The global Keyword Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.

Can you declare global variables in C?

The C language allows the redeclaration of the global variable. It means that this variable can get declared again when the first declaration doesn't lead to the initialization of the variable. It is possible because the second program works pretty well in the C language even if the first one fails during compilation.

Where we can declare global variable?

Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.


2 Answers

I have read that any variable declared outside a function is a global variable. I have done so, but in another *.cpp File that variable could not be found. So it was not realy global.

According to the concept of scope, your variable is global. However, what you've read/understood is overly-simplified.


Possibility 1

Perhaps you forgot to declare the variable in the other translation unit (TU). Here's an example:

a.cpp

int x = 5; // declaration and definition of my global variable 

b.cpp

// I want to use `x` here, too. // But I need b.cpp to know that it exists, first: extern int x; // declaration (not definition)  void foo() {    cout << x;  // OK } 

Typically you'd place extern int x; in a header file that gets included into b.cpp, and also into any other TU that ends up needing to use x.


Possibility 2

Additionally, it's possible that the variable has internal linkage, meaning that it's not exposed across translation units. This will be the case by default if the variable is marked const ([C++11: 3.5/3]):

a.cpp

const int x = 5; // file-`static` by default, because `const` 

b.cpp

extern const int x;    // says there's a `x` that we can use somewhere...  void foo() {    cout << x;    // ... but actually there isn't. So, linker error. } 

You could fix this by applying extern to the definition, too:

a.cpp

extern const int x = 5; 

This whole malarky is roughly equivalent to the mess you go through making functions visible/usable across TU boundaries, but with some differences in how you go about it.

like image 160
Lightness Races in Orbit Avatar answered Sep 22 '22 06:09

Lightness Races in Orbit


You declare the variable as extern in a common header:

//globals.h extern int x; 

And define it in an implementation file.

//globals.cpp int x = 1337; 

You can then include the header everywhere you need access to it.

I suggest you also wrap the variable inside a namespace.

like image 37
Luchian Grigore Avatar answered Sep 22 '22 06:09

Luchian Grigore