I have a global variable in one of the cpp files, where I am assigning a value to it. Now in order to be able to use it in another cpp file, I am declaring it as extern
and this file has multiple functions that use it so I am doing this globally. Now the value of this variable can be accessed in one of the functions and not in the other one. Any suggestions except using it in a header file would be good because I wasted 4 days playing with that.
Asked
Active
Viewed 5.5k times
22

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278

Venushree Patel
- 279
- 1
- 4
- 6
-
4This one is nearly impossible to answer without a magic guessing hat, or seeing some code. – Sergey Kalinichenko Sep 05 '12 at 22:06
-
What are the symptoms? Compilation failure, crash when running? – Daniel Fischer Sep 05 '12 at 22:12
-
No , when I write out that variable to a file ( from within the function where I cannot access it ) it just prints null, i.e nothing – Venushree Patel Sep 05 '12 at 22:15
-
Possible duplicate of [Accessing a global variable defined in a .cpp file in another .cpp file](https://stackoverflow.com/questions/43798283/accessing-a-global-variable-defined-in-a-cpp-file-in-another-cpp-file) – CinCout Aug 22 '17 at 04:44
1 Answers
55
Sorry, I'm ignoring the request for answers suggesting anything other than the use of header files. This is what headers are for, when you use them correctly... Read carefully:
global.h
#ifndef MY_GLOBALS_H
#define MY_GLOBALS_H
// This is a declaration of your variable, which tells the linker this value
// is found elsewhere. Anyone who wishes to use it must include global.h,
// either directly or indirectly.
extern int myglobalint;
#endif
global.cpp
#include "global.h"
// This is the definition of your variable. It can only happen in one place.
// You must include global.h so that the compiler matches it to the correct
// one, and doesn't implicitly convert it to static.
int myglobalint = 0;
user.cpp
// Anyone who uses the global value must include the appropriate header.
#include "global.h"
void SomeFunction()
{
// Now you can access the variable.
int temp = myglobalint;
}
Now, when you compile and link your project, you must:
- Compile each source (.cpp) file into an object file;
- Link all object files to create your executable / library / whatever.
Using the syntax I have given above, you should have neither compile nor link errors.

paddy
- 60,864
- 6
- 61
- 103
-
2+1: Thanks — I was going to start an answer with 'the only correct way to deal with this is by using header files properly'. You've saved me the effort. The only comment I'd make is that in `global.cpp`, I'd probably put an explicit initializer (even if it is the default of 0), just to 'make sure'. I'll also mention that [What are `extern` variables in C](http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c/) applies to C++ with very few if any changes. – Jonathan Leffler Sep 05 '12 at 22:26
-
Cheers. I was trying to keep it bare-bones and left that out, but an initializer is probably a good idea. – paddy Sep 05 '12 at 22:31
-
1A little late... But you can compile and link at the same time by running `gcc file1.c file2.c -o myprogram`... – NerdOfCode Mar 18 '18 at 05:02
-
@NerdOfCode The `g++` and the `gcc` do not exactly behave the same in terms of inter-linkage such as the need of `extern "C"` and name mangling i would be careful and emphasize this very important difference – daparic Mar 15 '20 at 19:12
-
great answer! But a question, what if my variable is an object, for example a vector: what would be a good way of initializing it (is not enough with constructors)? Perhaps making a function in global.cpp that makes the initialization and from user.cpp calling that function before using the variables? – Eduardo Pascual Aseff Mar 27 '20 at 16:32
-
It's the same thing. Declare as extern in the header, define in the source file. If you want to initialize only when first used, then use a "getter" function that defines the static locally and returns a reference. This is also a common solution to the [static initialization order fiasco](https://stackoverflow.com/a/335746/1553090) – paddy Mar 28 '20 at 23:28
-
Old answer but very usefull. I'm starting to split my program into files by functionality, and I had this problem. – Daniel Carrasco Marín May 04 '21 at 20:48